Skip to content

Commit

Permalink
Rename fromEither to fromRight, add fromLeft and new fromEither
Browse files Browse the repository at this point in the history
  • Loading branch information
futpib committed Jul 31, 2019
1 parent 2be6336 commit 5044ad5
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 8 deletions.
56 changes: 51 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2320,24 +2320,70 @@
impl: isRight
};

//# fromEither :: b -> Either a b -> b
//# fromLeft :: a -> Either a b -> a
//.
//. Takes a default value and an Either, and returns the Left value
//. if the Either is a Left; the default value otherwise.
//.
//. See also [`fromRight`](#fromRight).
//.
//. ```javascript
//. > S.fromLeft (0) (S.Left (42))
//. 42
//.
//. > S.fromLeft (0) (S.Right (42))
//. 0
//. ```
function fromLeft(x) {
return either (I) (K (x));
}
_.fromLeft = {
consts: {},
types: [a, $.Either (a) (b), a],
impl: fromLeft
};

//# fromRight :: b -> Either a b -> b
//.
//. Takes a default value and an Either, and returns the Right value
//. if the Either is a Right; the default value otherwise.
//.
//. See also [`fromLeft`](#fromLeft).
//.
//. ```javascript
//. > S.fromEither (0) (S.Right (42))
//. > S.fromRight (0) (S.Right (42))
//. 42
//.
//. > S.fromEither (0) (S.Left (42))
//. > S.fromRight (0) (S.Left (42))
//. 0
//. ```
function fromEither(x) {
function fromRight(x) {
return either (K (x)) (I);
}
_.fromEither = {
_.fromRight = {
consts: {},
types: [b, $.Either (a) (b), b],
impl: fromRight
};

//# fromEither :: Either a a -> a
//.
//. Takes an Either with the same type on the Left and on the Right
//. and return whichever value exists.
//.
//. ```javascript
//. > S.fromEither (S.Right (42))
//. 42
//.
//. > S.fromEither (S.Left (42))
//. 42
//. ```
function fromEither(x) {
return either (I) (I) (x);
}
_.fromEither = {
consts: {},
types: [$.Either (a) (a), a],
impl: fromEither
};

Expand Down
6 changes: 3 additions & 3 deletions test/fromEither.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const eq = require ('./internal/eq');

test ('fromEither', () => {

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

eq (S.fromEither (0) (S.Left (42))) (0);
eq (S.fromEither (0) (S.Right (42))) (42);
eq (S.fromEither (S.Left (42))) (42);
eq (S.fromEither (S.Right (42))) (42);

});
15 changes: 15 additions & 0 deletions test/fromLeft.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const S = require ('..');

const eq = require ('./internal/eq');


test ('fromLeft', () => {

eq (S.show (S.fromLeft)) ('fromLeft :: a -> Either a b -> a');

eq (S.fromLeft (0) (S.Left (42))) (42);
eq (S.fromLeft (0) (S.Right (42))) (0);

});
15 changes: 15 additions & 0 deletions test/fromRight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const S = require ('..');

const eq = require ('./internal/eq');


test ('fromRight', () => {

eq (S.show (S.fromRight)) ('fromRight :: b -> Either a b -> b');

eq (S.fromRight (0) (S.Left (42))) (0);
eq (S.fromRight (0) (S.Right (42))) (42);

});

0 comments on commit 5044ad5

Please sign in to comment.