Skip to content

Commit

Permalink
Rename eitherToMaybe to rightToMaybe and add leftToMaybe
Browse files Browse the repository at this point in the history
  • Loading branch information
futpib committed Aug 9, 2020
1 parent 161a7e1 commit c7e798c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
43 changes: 34 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2232,7 +2232,7 @@
//. Converts a Maybe to an Either. Nothing becomes a Right (containing the
//. first argument); a Just becomes a Left.
//.
//. See also [`eitherToMaybe`](#eitherToMaybe) and
//. See also [`leftToMaybe`](#leftToMaybe) and
// [`maybeToRight`](#maybeToRight).
//.
//. ```javascript
Expand All @@ -2256,7 +2256,7 @@
//. Converts a Maybe to an Either. Nothing becomes a Left (containing the
//. first argument); a Just becomes a Right.
//.
//. See also [`eitherToMaybe`](#eitherToMaybe) and
//. See also [`rightToMaybe`](#rightToMaybe) and
// [`maybeToLeft`](#maybeToLeft).
//.
//. ```javascript
Expand Down Expand Up @@ -2535,27 +2535,52 @@
impl: encase
};

//# eitherToMaybe :: Either a b -> Maybe b
//# leftToMaybe :: Either a b -> Maybe a
//.
//. Converts an Either to a Maybe. A Left becomes a Just; a Right becomes
//. Nothing.
//.
//. See also [`maybeToLeft`](#maybeToLeft) and
//. [`rightToMaybe`](#rightToMaybe).
//.
//. ```javascript
//. > S.leftToMaybe (S.Left ('Cannot divide by zero'))
//. Just ('Cannot divide by zero')
//.
//. > S.leftToMaybe (S.Right (42))
//. Nothing
//. ```
function leftToMaybe(either) {
return either.isLeft ? Just (either.value) : Nothing;
}
_.leftToMaybe = {
consts: {},
types: [$.Either (a) (b), $.Maybe (a)],
impl: leftToMaybe
};

//# rightToMaybe :: Either a b -> Maybe b
//.
//. Converts an Either to a Maybe. A Left becomes Nothing; a Right becomes
//. a Just.
//.
//. See also [`maybeToRight`](#maybeToRight).
//. See also [`maybeToRight`](#maybeToRight) and
//. [`leftToMaybe`](#leftToMaybe).
//.
//. ```javascript
//. > S.eitherToMaybe (S.Left ('Cannot divide by zero'))
//. > S.rightToMaybe (S.Left ('Cannot divide by zero'))
//. Nothing
//.
//. > S.eitherToMaybe (S.Right (42))
//. > S.rightToMaybe (S.Right (42))
//. Just (42)
//. ```
function eitherToMaybe(either) {
function rightToMaybe(either) {
return either.isLeft ? Nothing : Just (either.value);
}
_.eitherToMaybe = {
_.rightToMaybe = {
consts: {},
types: [$.Either (a) (b), $.Maybe (b)],
impl: eitherToMaybe
impl: rightToMaybe
};

//. ### Logic
Expand Down
15 changes: 15 additions & 0 deletions test/leftToMaybe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const S = require ('..');

const eq = require ('./internal/eq');


test ('leftToMaybe', () => {

eq (S.show (S.leftToMaybe)) ('leftToMaybe :: Either a b -> Maybe a');

eq (S.leftToMaybe (S.Left ('Cannot divide by zero'))) (S.Just ('Cannot divide by zero'));
eq (S.leftToMaybe (S.Right (42))) (S.Nothing);

});
15 changes: 15 additions & 0 deletions test/rightToMaybe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const S = require ('..');

const eq = require ('./internal/eq');


test ('rightToMaybe', () => {

eq (S.show (S.rightToMaybe)) ('rightToMaybe :: Either a b -> Maybe b');

eq (S.rightToMaybe (S.Left ('Cannot divide by zero'))) (S.Nothing);
eq (S.rightToMaybe (S.Right (42))) (S.Just (42));

});

0 comments on commit c7e798c

Please sign in to comment.