Skip to content

Commit 0a707c0

Browse files
committed
Add new fromEither
1 parent fd7a023 commit 0a707c0

File tree

2 files changed

+21
-27
lines changed

2 files changed

+21
-27
lines changed

index.js

+18-24
Original file line numberDiff line numberDiff line change
@@ -2394,14 +2394,14 @@
23942394
//. Takes a default value and an Either, and returns the Left value
23952395
//. if the Either is a Left; the default value otherwise.
23962396
//.
2397-
//. See also [`either`](#either) and [`fromRight`](#fromRight).
2397+
//. See also [`fromRight`](#fromRight).
23982398
//.
23992399
//. ```javascript
2400-
//. > S.fromLeft ('abc') (S.Left ('xyz'))
2401-
//. 'xyz'
2400+
//. > S.fromLeft (0) (S.Left (42))
2401+
//. 42
24022402
//.
2403-
//. > S.fromLeft ('abc') (S.Right (123))
2404-
//. 'abc'
2403+
//. > S.fromLeft (0) (S.Right (42))
2404+
//. 0
24052405
//. ```
24062406
function fromLeft(x) {
24072407
return either (I) (K (x));
@@ -2417,14 +2417,14 @@
24172417
//. Takes a default value and an Either, and returns the Right value
24182418
//. if the Either is a Right; the default value otherwise.
24192419
//.
2420-
//. See also [`either`](#either) and [`fromLeft`](#fromLeft).
2420+
//. See also [`fromLeft`](#fromLeft).
24212421
//.
24222422
//. ```javascript
2423-
//. > S.fromRight (123) (S.Right (789))
2424-
//. 789
2423+
//. > S.fromRight (0) (S.Right (42))
2424+
//. 42
24252425
//.
2426-
//. > S.fromRight (123) (S.Left ('abc'))
2427-
//. 123
2426+
//. > S.fromRight (0) (S.Left (42))
2427+
//. 0
24282428
//. ```
24292429
function fromRight(x) {
24302430
return either (K (x)) (I);
@@ -2435,28 +2435,22 @@
24352435
impl: fromRight
24362436
};
24372437

2438-
//# fromEither :: b -> Either a b -> b
2439-
//.
2440-
//. Takes a default value and an Either, and returns the Right value
2441-
//. if the Either is a Right; the default value otherwise.
2438+
//# fromEither :: Either a a -> a
24422439
//.
2443-
//. The behaviour of `fromEither` is likely to change in a future release.
2444-
//. Please use [`fromRight`](#fromRight) instead.
2440+
//. Takes an Either with the same type on the Left and on the Right
2441+
//. and return whichever value exists.
24452442
//.
24462443
//. ```javascript
2447-
//. > S.fromEither (0) (S.Right (42))
2444+
//. > S.fromEither (S.Right (42))
24482445
//. 42
24492446
//.
2450-
//. > S.fromEither (0) (S.Left (42))
2451-
//. 0
2447+
//. > S.fromEither (S.Left (42))
2448+
//. 42
24522449
//. ```
2453-
function fromEither(x) {
2454-
return either (K (x)) (I);
2455-
}
24562450
_.fromEither = {
24572451
consts: {},
2458-
types: [b, $.Either (a) (b), b],
2459-
impl: fromEither
2452+
types: [$.Either (a) (a), a],
2453+
impl: either (I) (I)
24602454
};
24612455

24622456
//# lefts :: (Filterable f, Functor f) => f (Either a b) -> f a

test/fromEither.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ const eq = require ('./internal/eq');
77

88
test ('fromEither', () => {
99

10-
eq (S.show (S.fromEither)) ('fromEither :: b -> Either a b -> b');
10+
eq (S.show (S.fromEither)) ('fromEither :: Either a a -> a');
1111

12-
eq (S.fromEither (0) (S.Left (42))) (0);
13-
eq (S.fromEither (0) (S.Right (42))) (42);
12+
eq (S.fromEither (S.Left (42))) (42);
13+
eq (S.fromEither (S.Right (42))) (42);
1414

1515
});

0 commit comments

Comments
 (0)