diff --git a/index.js b/index.js index 62c89f59..081ae2d9 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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 @@ -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 @@ -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: {}, diff --git a/test/eitherToMaybe.js b/test/eitherToMaybe.js deleted file mode 100644 index 7d61bbff..00000000 --- a/test/eitherToMaybe.js +++ /dev/null @@ -1,15 +0,0 @@ -'use strict'; - -const S = require ('..'); - -const eq = require ('./internal/eq'); - - -test ('eitherToMaybe', () => { - - eq (S.show (S.eitherToMaybe)) ('eitherToMaybe :: Either a b -> Maybe b'); - - eq (S.eitherToMaybe (S.Left ('Cannot divide by zero'))) (S.Nothing); - eq (S.eitherToMaybe (S.Right (42))) (S.Just (42)); - -}); diff --git a/test/leftToMaybe.js b/test/leftToMaybe.js new file mode 100644 index 00000000..37546d79 --- /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/rightToMaybe.js b/test/rightToMaybe.js new file mode 100644 index 00000000..ea23df25 --- /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)); + +});