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

Add EitherMaybe conversion functions #644

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
96 changes: 96 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2145,6 +2145,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 [`leftToMaybe`](#leftToMaybe) 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)
Copy link
Member

Choose a reason for hiding this comment

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

These are nice examples. :)

//. ```
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 [`rightToMaybe`](#rightToMaybe) 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 @@ -2401,6 +2449,54 @@
impl: encase
};

//# 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) and
//. [`leftToMaybe`](#leftToMaybe).
//.
//. ```javascript
//. > S.rightToMaybe (S.Left ('Cannot divide by zero'))
//. Nothing
//.
//. > S.rightToMaybe (S.Right (42))
//. Just (42)
//. ```
function rightToMaybe(either) {
return either.isLeft ? Nothing : Just (either.value);
}
_.rightToMaybe = {
consts: {},
types: [$.Either (a) (b), $.Maybe (b)],
impl: rightToMaybe
};

//. ### Logic

//# and :: Boolean -> Boolean -> Boolean
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/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));

});
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));

});