Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

either: implement Foldable #204

Merged
merged 1 commit into from
May 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@
[b, $Maybe(b)],
Maybe.of);

//# Maybe#reduce :: Maybe a ~> (b -> a -> b) -> b -> b
//# Maybe#reduce :: Maybe a ~> ((b, a) -> b) -> b -> b
//.
//. Takes a function and an initial value of any type, and returns:
//.
Expand Down Expand Up @@ -1357,8 +1357,8 @@
//. `Either a b` is either a Left whose value is of type `a` or a Right whose
//. value is of type `b`.
//.
//. The Either type satisfies the [Semigroup][], [Monad][], and [Extend][]
//. specifications.
//. The Either type satisfies the [Semigroup][], [Monad][], [Foldable][], and
//. [Extend][] specifications.

//# EitherType :: Type -> Type -> Type
//.
Expand Down Expand Up @@ -1591,6 +1591,30 @@
[c, $Either(a, c)],
Either.of);

//# Either#reduce :: Either a b ~> ((c, b) -> c) -> c -> c
//.
//. Takes a function and an initial value of any type, and returns:
//.
//. - the initial value if `this` is a Left; otherwise
//.
//. - the result of applying the function to the initial value and this
//. Right's value.
//.
//. ```javascript
//. > S.Left('Cannot divide by zero').reduce((xs, x) => xs.concat([x]), [42])
//. [42]
//.
//. > S.Right(5).reduce((xs, x) => xs.concat([x]), [42])
//. [42, 5]
//. ```
Either.prototype.reduce =
method('Either#reduce',
{},
[$Either(a, b), $.Function, c, c],
function(either, f, x) {
return either.isRight ? f(x, either.value) : x;
});

//# Either#toBoolean :: Either a b ~> Boolean
//.
//. Returns `false` if `this` is a Left; `true` if `this` is a Right.
Expand Down
18 changes: 18 additions & 0 deletions test/Either/Left.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,24 @@ describe('Left', function() {
'The value at position 1 is not a member of ‘Function’.\n'));
});

it('provides a "reduce" method', function() {
eq(S.Left().reduce.length, 2);
eq(S.Left().reduce(function(xs, x) { return xs.concat([x]); }, [42]),
[42]);

throws(function() { S.Left().reduce(null, null); },
errorEq(TypeError,
'Invalid value\n' +
'\n' +
'Either#reduce :: Either a b -> Function -> c -> c\n' +
' ^^^^^^^^\n' +
' 1\n' +
'\n' +
'1) null :: Null\n' +
'\n' +
'The value at position 1 is not a member of ‘Function’.\n'));
});

it('provides a "toBoolean" method', function() {
eq(S.Left('abc').toBoolean.length, 0);
eq(S.Left('abc').toBoolean(), false);
Expand Down
18 changes: 18 additions & 0 deletions test/Either/Right.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,24 @@ describe('Right', function() {
'The value at position 1 is not a member of ‘Function’.\n'));
});

it('provides a "reduce" method', function() {
eq(S.Right(5).reduce.length, 2);
eq(S.Right(5).reduce(function(xs, x) { return xs.concat([x]); }, [42]),
[42, 5]);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually a type error, which I noticed thanks to #232. :)


throws(function() { S.Right().reduce(null, null); },
errorEq(TypeError,
'Invalid value\n' +
'\n' +
'Either#reduce :: Either a b -> Function -> c -> c\n' +
' ^^^^^^^^\n' +
' 1\n' +
'\n' +
'1) null :: Null\n' +
'\n' +
'The value at position 1 is not a member of ‘Function’.\n'));
});

it('provides a "toBoolean" method', function() {
eq(S.Right(42).toBoolean.length, 0);
eq(S.Right(42).toBoolean(), true);
Expand Down