Skip to content

Commit

Permalink
Rename maybeToEither to maybeToRight and add maybeToLeft
Browse files Browse the repository at this point in the history
  • Loading branch information
futpib committed Aug 9, 2020
1 parent eba8d71 commit 161a7e1
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
71 changes: 71 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2227,6 +2227,54 @@
impl: maybeToNullable
};

//# maybeToLeft :: b -> Maybe a -> Either a b
//.
//. Converts a Maybe to an Either. Nothing becomes a Right (containing the
//. first argument); a Just becomes a Left.
//.
//. See also [`eitherToMaybe`](#eitherToMaybe) and
// [`maybeToRight`](#maybeToRight).
//.
//. ```javascript
//. > S.maybeToLeft ('No negative numbers') (S.find (S.lt (0)) ([0, 1, 2]))
//. Right ('No negative numbers')
//.
//. > S.maybeToLeft ('No negative numbers') (S.find (S.lt (0)) ([-1, 0, 1]))
//. Left (-1)
//. ```
function maybeToLeft(x) {
return maybe (Right (x)) (Left);
}
_.maybeToLeft = {
consts: {},
types: [b, $.Maybe (a), $.Either (a) (b)],
impl: maybeToLeft
};

//# maybeToRight :: a -> Maybe b -> Either a b
//.
//. Converts a Maybe to an Either. Nothing becomes a Left (containing the
//. first argument); a Just becomes a Right.
//.
//. See also [`eitherToMaybe`](#eitherToMaybe) and
// [`maybeToLeft`](#maybeToLeft).
//.
//. ```javascript
//. > S.maybeToRight ('Expecting an integer') (S.parseInt (10) ('xyz'))
//. Left ('Expecting an integer')
//.
//. > S.maybeToRight ('Expecting an integer') (S.parseInt (10) ('42'))
//. Right (42)
//. ```
function maybeToRight(x) {
return maybe (Left (x)) (Right);
}
_.maybeToRight = {
consts: {},
types: [a, $.Maybe (b), $.Either (a) (b)],
impl: maybeToRight
};

//. ### Either
//.
//. The Either type represents values with two possibilities: a value of type
Expand Down Expand Up @@ -2487,6 +2535,29 @@
impl: encase
};

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

//. ### Logic

//# and :: Boolean -> Boolean -> Boolean
Expand Down
15 changes: 15 additions & 0 deletions test/maybeToLeft.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 ('maybeToLeft', () => {

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

eq (S.maybeToLeft ('success msg') (S.Nothing)) (S.Right ('success msg'));
eq (S.maybeToLeft ('success msg') (S.Just (42))) (S.Left (42));

});
15 changes: 15 additions & 0 deletions test/maybeToRight.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 ('maybeToRight', () => {

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

eq (S.maybeToRight ('error msg') (S.Nothing)) (S.Left ('error msg'));
eq (S.maybeToRight ('error msg') (S.Just (42))) (S.Right (42));

});

0 comments on commit 161a7e1

Please sign in to comment.