Skip to content

Commit

Permalink
Add fromLeft and fromRight
Browse files Browse the repository at this point in the history
  • Loading branch information
futpib authored and davidchambers committed Jul 26, 2020
1 parent c69b661 commit ec92fe5
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
46 changes: 46 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2362,6 +2362,52 @@
impl: either
};

//# 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.fromRight (0) (S.Right (42))
//. 42
//.
//. > S.fromRight (0) (S.Left (42))
//. 0
//. ```
function fromRight(x) {
return either (K (x)) (I);
}
_.fromRight = {
consts: {},
types: [b, $.Either (a) (b), b],
impl: fromRight
};

//# fromEither :: b -> Either a b -> b
//.
//. Takes a default value and an Either, and returns the Right value
Expand Down
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 ec92fe5

Please sign in to comment.