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 Jul 31, 2019
1 parent e9bf697 commit 7a28ebe
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 25 deletions.
45 changes: 35 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2197,7 +2197,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 @@ -2221,7 +2221,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 @@ -2492,27 +2492,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 Expand Up @@ -4325,7 +4350,7 @@
//. Just ([1, 2, 3])
//. ```
function parseJson(pred) {
return B (filter (pred)) (B (eitherToMaybe) (encase (JSON.parse)));
return B (filter (pred)) (B (rightToMaybe) (encase (JSON.parse)));
}
_.parseJson = {
consts: {},
Expand Down
15 changes: 0 additions & 15 deletions test/eitherToMaybe.js

This file was deleted.

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 7a28ebe

Please sign in to comment.