diff --git a/index.js b/index.js index a8c244b39..66ca9b056 100644 --- a/index.js +++ b/index.js @@ -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) + //. ``` + 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 @@ -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 diff --git a/test/leftToMaybe.js b/test/leftToMaybe.js new file mode 100644 index 000000000..37546d79a --- /dev/null +++ b/test/leftToMaybe.js @@ -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); + +}); diff --git a/test/maybeToLeft.js b/test/maybeToLeft.js new file mode 100644 index 000000000..c9120e393 --- /dev/null +++ b/test/maybeToLeft.js @@ -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)); + +}); diff --git a/test/maybeToRight.js b/test/maybeToRight.js new file mode 100644 index 000000000..d2b68b579 --- /dev/null +++ b/test/maybeToRight.js @@ -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)); + +}); diff --git a/test/rightToMaybe.js b/test/rightToMaybe.js new file mode 100644 index 000000000..ea23df251 --- /dev/null +++ b/test/rightToMaybe.js @@ -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)); + +});