From 78db20d6c65acbcc7902c62bc9ded01fd0b3d10b Mon Sep 17 00:00:00 2001 From: Gabe Johnson Date: Fri, 16 Feb 2018 16:02:33 -0600 Subject: [PATCH] Change example `Identity` -> `Maybe` --- README.md | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 73eddad..1f760da 100644 --- a/README.md +++ b/README.md @@ -812,26 +812,45 @@ identity :: Identity i => i a ~> (a -> b) -> b Additionally, data constructors may be referenced in the specification by name and arity. Conforming data structures are not required to provide these constructors nor are any provided constructors required to share these names. -For example, instead of `Left` and `Right`, the constructors could be named -`Failure` and `Success`. +Instead of `Just` and `Nothing`, the constructors could be named `Some` and +`None`. For example ```js -function Id(x) { +function Some(x) { + if (!(this instanceof Some)) return new Some(x); this.x = x; -} +}; +var None = {}; -Id.prototype['fantasy-land/identity'] = function identity(f) { +Some.prototype['fantasy-land/maybe'] = function maybe(_, f) { return f(this.x); }; +None['fantasy-land/maybe'] = function maybe(d, _) { + return d; +}; -Array.prototype['fantasy-land/identity'] = function identity(f) { - return f(this[0]); +Array.prototype['fantasy-land/maybe'] = function maybe(d, f) { + return this.length === 0 ? d : f(this[0]); }; -var identity = 'fantasy-land/identity'; +function maybe(d, f, m) { + return m['fantasy-land/maybe'](d, f); +} + +var head = maybe.bind(null, None, Some); + +var none = head([]); +none // None -(new Id(42))[identity](x => x) === [42][identity](x => x); +var some = head([1, 2, 3]); +some // Some(1); + +function fromMaybe(d, m) { + return maybe(d, x => x, m); +} +fromMaybe(0, none) // 0 +fromMaybe(0, some) // 1 ``` ### Maybe