Skip to content

Commit

Permalink
Change example Identity -> Maybe
Browse files Browse the repository at this point in the history
  • Loading branch information
gabejohnson committed Feb 16, 2018
1 parent 010c84f commit 78db20d
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 78db20d

Please sign in to comment.