Skip to content

Commit 161a7e1

Browse files
committed
Rename maybeToEither to maybeToRight and add maybeToLeft
1 parent eba8d71 commit 161a7e1

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

index.js

+71
Original file line numberDiff line numberDiff line change
@@ -2227,6 +2227,54 @@
22272227
impl: maybeToNullable
22282228
};
22292229

2230+
//# maybeToLeft :: b -> Maybe a -> Either a b
2231+
//.
2232+
//. Converts a Maybe to an Either. Nothing becomes a Right (containing the
2233+
//. first argument); a Just becomes a Left.
2234+
//.
2235+
//. See also [`eitherToMaybe`](#eitherToMaybe) and
2236+
// [`maybeToRight`](#maybeToRight).
2237+
//.
2238+
//. ```javascript
2239+
//. > S.maybeToLeft ('No negative numbers') (S.find (S.lt (0)) ([0, 1, 2]))
2240+
//. Right ('No negative numbers')
2241+
//.
2242+
//. > S.maybeToLeft ('No negative numbers') (S.find (S.lt (0)) ([-1, 0, 1]))
2243+
//. Left (-1)
2244+
//. ```
2245+
function maybeToLeft(x) {
2246+
return maybe (Right (x)) (Left);
2247+
}
2248+
_.maybeToLeft = {
2249+
consts: {},
2250+
types: [b, $.Maybe (a), $.Either (a) (b)],
2251+
impl: maybeToLeft
2252+
};
2253+
2254+
//# maybeToRight :: a -> Maybe b -> Either a b
2255+
//.
2256+
//. Converts a Maybe to an Either. Nothing becomes a Left (containing the
2257+
//. first argument); a Just becomes a Right.
2258+
//.
2259+
//. See also [`eitherToMaybe`](#eitherToMaybe) and
2260+
// [`maybeToLeft`](#maybeToLeft).
2261+
//.
2262+
//. ```javascript
2263+
//. > S.maybeToRight ('Expecting an integer') (S.parseInt (10) ('xyz'))
2264+
//. Left ('Expecting an integer')
2265+
//.
2266+
//. > S.maybeToRight ('Expecting an integer') (S.parseInt (10) ('42'))
2267+
//. Right (42)
2268+
//. ```
2269+
function maybeToRight(x) {
2270+
return maybe (Left (x)) (Right);
2271+
}
2272+
_.maybeToRight = {
2273+
consts: {},
2274+
types: [a, $.Maybe (b), $.Either (a) (b)],
2275+
impl: maybeToRight
2276+
};
2277+
22302278
//. ### Either
22312279
//.
22322280
//. The Either type represents values with two possibilities: a value of type
@@ -2487,6 +2535,29 @@
24872535
impl: encase
24882536
};
24892537

2538+
//# eitherToMaybe :: Either a b -> Maybe b
2539+
//.
2540+
//. Converts an Either to a Maybe. A Left becomes Nothing; a Right becomes
2541+
//. a Just.
2542+
//.
2543+
//. See also [`maybeToRight`](#maybeToRight).
2544+
//.
2545+
//. ```javascript
2546+
//. > S.eitherToMaybe (S.Left ('Cannot divide by zero'))
2547+
//. Nothing
2548+
//.
2549+
//. > S.eitherToMaybe (S.Right (42))
2550+
//. Just (42)
2551+
//. ```
2552+
function eitherToMaybe(either) {
2553+
return either.isLeft ? Nothing : Just (either.value);
2554+
}
2555+
_.eitherToMaybe = {
2556+
consts: {},
2557+
types: [$.Either (a) (b), $.Maybe (b)],
2558+
impl: eitherToMaybe
2559+
};
2560+
24902561
//. ### Logic
24912562

24922563
//# and :: Boolean -> Boolean -> Boolean

test/maybeToLeft.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const S = require ('..');
4+
5+
const eq = require ('./internal/eq');
6+
7+
8+
test ('maybeToLeft', () => {
9+
10+
eq (S.show (S.maybeToLeft)) ('maybeToLeft :: b -> Maybe a -> Either a b');
11+
12+
eq (S.maybeToLeft ('success msg') (S.Nothing)) (S.Right ('success msg'));
13+
eq (S.maybeToLeft ('success msg') (S.Just (42))) (S.Left (42));
14+
15+
});

test/maybeToRight.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const S = require ('..');
4+
5+
const eq = require ('./internal/eq');
6+
7+
8+
test ('maybeToRight', () => {
9+
10+
eq (S.show (S.maybeToRight)) ('maybeToRight :: a -> Maybe b -> Either a b');
11+
12+
eq (S.maybeToRight ('error msg') (S.Nothing)) (S.Left ('error msg'));
13+
eq (S.maybeToRight ('error msg') (S.Just (42))) (S.Right (42));
14+
15+
});

0 commit comments

Comments
 (0)