Skip to content

Commit f7a9689

Browse files
committed
Rename eitherToMaybe to rightToMaybe and add leftToMaybe
1 parent 5044ad5 commit f7a9689

File tree

4 files changed

+65
-25
lines changed

4 files changed

+65
-25
lines changed

index.js

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,7 +2197,7 @@
21972197
//. Converts a Maybe to an Either. Nothing becomes a Right (containing the
21982198
//. first argument); a Just becomes a Left.
21992199
//.
2200-
//. See also [`eitherToMaybe`](#eitherToMaybe) and
2200+
//. See also [`leftToMaybe`](#leftToMaybe) and
22012201
// [`maybeToRight`](#maybeToRight).
22022202
//.
22032203
//. ```javascript
@@ -2221,7 +2221,7 @@
22212221
//. Converts a Maybe to an Either. Nothing becomes a Left (containing the
22222222
//. first argument); a Just becomes a Right.
22232223
//.
2224-
//. See also [`eitherToMaybe`](#eitherToMaybe) and
2224+
//. See also [`rightToMaybe`](#rightToMaybe) and
22252225
// [`maybeToLeft`](#maybeToLeft).
22262226
//.
22272227
//. ```javascript
@@ -2495,27 +2495,52 @@
24952495
impl: encase
24962496
};
24972497

2498-
//# eitherToMaybe :: Either a b -> Maybe b
2498+
//# leftToMaybe :: Either a b -> Maybe a
2499+
//.
2500+
//. Converts an Either to a Maybe. A Left becomes a Just; a Right becomes
2501+
//. Nothing.
2502+
//.
2503+
//. See also [`maybeToLeft`](#maybeToLeft) and
2504+
//. [`rightToMaybe`](#rightToMaybe).
2505+
//.
2506+
//. ```javascript
2507+
//. > S.leftToMaybe (S.Left ('Cannot divide by zero'))
2508+
//. Just ('Cannot divide by zero')
2509+
//.
2510+
//. > S.leftToMaybe (S.Right (42))
2511+
//. Nothing
2512+
//. ```
2513+
function leftToMaybe(either) {
2514+
return either.isLeft ? Just (either.value) : Nothing;
2515+
}
2516+
_.leftToMaybe = {
2517+
consts: {},
2518+
types: [$.Either (a) (b), $.Maybe (a)],
2519+
impl: leftToMaybe
2520+
};
2521+
2522+
//# rightToMaybe :: Either a b -> Maybe b
24992523
//.
25002524
//. Converts an Either to a Maybe. A Left becomes Nothing; a Right becomes
25012525
//. a Just.
25022526
//.
2503-
//. See also [`maybeToRight`](#maybeToRight).
2527+
//. See also [`maybeToRight`](#maybeToRight) and
2528+
//. [`leftToMaybe`](#leftToMaybe).
25042529
//.
25052530
//. ```javascript
2506-
//. > S.eitherToMaybe (S.Left ('Cannot divide by zero'))
2531+
//. > S.rightToMaybe (S.Left ('Cannot divide by zero'))
25072532
//. Nothing
25082533
//.
2509-
//. > S.eitherToMaybe (S.Right (42))
2534+
//. > S.rightToMaybe (S.Right (42))
25102535
//. Just (42)
25112536
//. ```
2512-
function eitherToMaybe(either) {
2537+
function rightToMaybe(either) {
25132538
return either.isLeft ? Nothing : Just (either.value);
25142539
}
2515-
_.eitherToMaybe = {
2540+
_.rightToMaybe = {
25162541
consts: {},
25172542
types: [$.Either (a) (b), $.Maybe (b)],
2518-
impl: eitherToMaybe
2543+
impl: rightToMaybe
25192544
};
25202545

25212546
//. ### Logic
@@ -4328,7 +4353,7 @@
43284353
//. Just ([1, 2, 3])
43294354
//. ```
43304355
function parseJson(pred) {
4331-
return B (filter (pred)) (B (eitherToMaybe) (encase (JSON.parse)));
4356+
return B (filter (pred)) (B (rightToMaybe) (encase (JSON.parse)));
43324357
}
43334358
_.parseJson = {
43344359
consts: {},

test/eitherToMaybe.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

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)