Skip to content

Commit f3d6f71

Browse files
committed
Rename eitherToMaybe to rightToMaybe and add leftToMaybe
1 parent 478bb89 commit f3d6f71

File tree

3 files changed

+64
-9
lines changed

3 files changed

+64
-9
lines changed

index.js

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,7 +2232,7 @@
22322232
//. Converts a Maybe to an Either. Nothing becomes a Right (containing the
22332233
//. first argument); a Just becomes a Left.
22342234
//.
2235-
//. See also [`eitherToMaybe`](#eitherToMaybe) and
2235+
//. See also [`leftToMaybe`](#leftToMaybe) and
22362236
// [`maybeToRight`](#maybeToRight).
22372237
//.
22382238
//. ```javascript
@@ -2256,7 +2256,7 @@
22562256
//. Converts a Maybe to an Either. Nothing becomes a Left (containing the
22572257
//. first argument); a Just becomes a Right.
22582258
//.
2259-
//. See also [`eitherToMaybe`](#eitherToMaybe) and
2259+
//. See also [`rightToMaybe`](#rightToMaybe) and
22602260
// [`maybeToLeft`](#maybeToLeft).
22612261
//.
22622262
//. ```javascript
@@ -2529,27 +2529,52 @@
25292529
impl: encase
25302530
};
25312531

2532-
//# eitherToMaybe :: Either a b -> Maybe b
2532+
//# leftToMaybe :: Either a b -> Maybe a
2533+
//.
2534+
//. Converts an Either to a Maybe. A Left becomes a Just; a Right becomes
2535+
//. Nothing.
2536+
//.
2537+
//. See also [`maybeToLeft`](#maybeToLeft) and
2538+
//. [`rightToMaybe`](#rightToMaybe).
2539+
//.
2540+
//. ```javascript
2541+
//. > S.leftToMaybe (S.Left ('Cannot divide by zero'))
2542+
//. Just ('Cannot divide by zero')
2543+
//.
2544+
//. > S.leftToMaybe (S.Right (42))
2545+
//. Nothing
2546+
//. ```
2547+
function leftToMaybe(either) {
2548+
return either.isLeft ? Just (either.value) : Nothing;
2549+
}
2550+
_.leftToMaybe = {
2551+
consts: {},
2552+
types: [$.Either (a) (b), $.Maybe (a)],
2553+
impl: leftToMaybe
2554+
};
2555+
2556+
//# rightToMaybe :: Either a b -> Maybe b
25332557
//.
25342558
//. Converts an Either to a Maybe. A Left becomes Nothing; a Right becomes
25352559
//. a Just.
25362560
//.
2537-
//. See also [`maybeToRight`](#maybeToRight).
2561+
//. See also [`maybeToRight`](#maybeToRight) and
2562+
//. [`leftToMaybe`](#leftToMaybe).
25382563
//.
25392564
//. ```javascript
2540-
//. > S.eitherToMaybe (S.Left ('Cannot divide by zero'))
2565+
//. > S.rightToMaybe (S.Left ('Cannot divide by zero'))
25412566
//. Nothing
25422567
//.
2543-
//. > S.eitherToMaybe (S.Right (42))
2568+
//. > S.rightToMaybe (S.Right (42))
25442569
//. Just (42)
25452570
//. ```
2546-
function eitherToMaybe(either) {
2571+
function rightToMaybe(either) {
25472572
return either.isLeft ? Nothing : Just (either.value);
25482573
}
2549-
_.eitherToMaybe = {
2574+
_.rightToMaybe = {
25502575
consts: {},
25512576
types: [$.Either (a) (b), $.Maybe (b)],
2552-
impl: eitherToMaybe
2577+
impl: rightToMaybe
25532578
};
25542579

25552580
//. ### Logic

test/leftToMaybe.js

Lines changed: 15 additions & 0 deletions
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 ('leftToMaybe', () => {
9+
10+
eq (S.show (S.leftToMaybe)) ('leftToMaybe :: Either a b -> Maybe a');
11+
12+
eq (S.leftToMaybe (S.Left ('Cannot divide by zero'))) (S.Just ('Cannot divide by zero'));
13+
eq (S.leftToMaybe (S.Right (42))) (S.Nothing);
14+
15+
});

test/rightToMaybe.js

Lines changed: 15 additions & 0 deletions
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 ('rightToMaybe', () => {
9+
10+
eq (S.show (S.rightToMaybe)) ('rightToMaybe :: Either a b -> Maybe b');
11+
12+
eq (S.rightToMaybe (S.Left ('Cannot divide by zero'))) (S.Nothing);
13+
eq (S.rightToMaybe (S.Right (42))) (S.Just (42));
14+
15+
});

0 commit comments

Comments
 (0)