-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #58
- Loading branch information
Showing
4 changed files
with
88 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { invoker } from 'ramda'; | ||
|
||
/** | ||
* The catamorphism for either. If the either is right than the right function will be executed with | ||
* the right value and the value of the function returned. Otherwise the left function | ||
* will be called with the left value. | ||
* | ||
* @func cata | ||
* @memberOf RA | ||
* @since {@link https://char0n.github.io/ramda-adjunct/1.4.0|v1.4.0} | ||
* @category Function | ||
* @sig (a -> b) -> (a -> c) -> Either a -> b | c | ||
* @param {*} val The value to test | ||
* @return {*} | ||
* @see {@link RA.isNotArray|isNotArray} | ||
* @example | ||
* | ||
* const eitherR = Either.Right(1); | ||
* const eitherL = Either.Left(2); | ||
* | ||
* RA.cata(identity, identity, eitherR); //=> 1 | ||
* RA.cata(identity, identity, eitherL); //=> 2 | ||
*/ | ||
const cata = invoker(2, 'cata'); | ||
|
||
export default cata; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Either } from 'monet'; | ||
import { identity } from 'ramda'; | ||
import chai from 'chai'; | ||
|
||
import RA from '../src/index'; | ||
import eq from './shared/eq'; | ||
|
||
describe('cata', function() { | ||
const eitherR = Either.Right(1); | ||
const eitherL = Either.Left(2); | ||
|
||
it('tests an Either for catamorphism', function() { | ||
eq(RA.cata(identity, identity, eitherR), 1); | ||
eq(RA.cata(identity, identity, eitherL), 2); | ||
}); | ||
|
||
it('test currying', function() { | ||
eq(RA.cata(identity)(identity)(eitherR), 1); | ||
eq(RA.cata(identity, identity)(eitherL), 2); | ||
}); | ||
|
||
it('tests left for catamorphism without functions', function() { | ||
chai.assert.throws(RA.cata.bind(null, null, identity, eitherL), TypeError); | ||
eq(RA.cata(identity, null, eitherL), 2); | ||
}); | ||
|
||
it('tests right for catamorphism without functions', function() { | ||
chai.assert.throws(RA.cata.bind(null, identity, null, eitherR), TypeError); | ||
eq(RA.cata(null, identity, eitherR), 1); | ||
}); | ||
}); |