From 99581e260cc032fb806e0f1898521b9e56b3227d Mon Sep 17 00:00:00 2001 From: David Chambers Date: Tue, 13 Sep 2016 12:03:16 +0200 Subject: [PATCH] =?UTF-8?q?specify=20location=20of=20=E2=80=98empty?= =?UTF-8?q?=E2=80=99=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An instance method is more flexible than a static method. It facilitates > Identity([1, 2, 3]).empty() Identity([]) since Identity#empty can dispatch to the inner value's ‘empty’ method. --- README.md | 5 ++--- id.js | 5 ++--- id_test.js | 7 +++---- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c93f5e3..29bb059 100644 --- a/README.md +++ b/README.md @@ -138,11 +138,10 @@ the Semigroup specification. empty :: Monoid m => () -> m ``` -A value which has a Monoid must provide an `empty` method on itself or -its `constructor` object. The `empty` method takes no arguments: +A value which has a Monoid must provide an `empty` method. The `empty` +method takes no arguments: m.empty() - m.constructor.empty() 1. `empty` must return a value of the same Monoid diff --git a/id.js b/id.js index 9b4e81d..168b4d1 100644 --- a/id.js +++ b/id.js @@ -15,10 +15,9 @@ Id.prototype[fl.concat] = function(b) { }; // Monoid (value must also be a Monoid) -Id[fl.empty] = function() { - return new Id(this.value[fl.empty] ? this.value[fl.empty]() : this.value.constructor[fl.empty]()); +Id.prototype[fl.empty] = function() { + return new Id(this.value[fl.empty]()); }; -Id.prototype[fl.empty] = Id[fl.empty]; // Foldable Id.prototype[fl.reduce] = function(f, acc) { diff --git a/id_test.js b/id_test.js index c683584..099d65f 100644 --- a/id_test.js +++ b/id_test.js @@ -22,9 +22,8 @@ const Id = require('./id'); // Special type of sum for the type of string. const Sum = tagged('v'); Sum[of] = (x) => Sum(x); -Sum[empty] = () => Sum(''); Sum.prototype[of] = Sum[of]; -Sum.prototype[empty] = Sum[empty]; +Sum.prototype[empty] = () => Sum(''); Sum.prototype[map] = function(f) { return Sum(f(this.v)); }; @@ -90,8 +89,8 @@ exports.monad = { }; exports.monoid = { - leftIdentity: test((x) => monoid.leftIdentity(Id[of](Sum[empty]()))(equality)(Sum[of](x))), - rightIdentity: test((x) => monoid.rightIdentity(Id[of](Sum[empty]()))(equality)(Sum[of](x))) + leftIdentity: test((x) => monoid.leftIdentity(Id[of](Sum.prototype[empty]()))(equality)(Sum[of](x))), + rightIdentity: test((x) => monoid.rightIdentity(Id[of](Sum.prototype[empty]()))(equality)(Sum[of](x))) }; // Semigroup tests are broken otherwise for this.