source: node_modules/ramda-adjunct/es/cata.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 3.3 KB
RevLine 
[d24f17c]1import { curry } from 'ramda';
2import isFunction from './isFunction';
3
4/**
5 * The catamorphism is a way of folding a type into a value.
6 *
7 * **Either**
8 *
9 * If the either is right then the right function will be executed with
10 * the `right` value and the value of the function returned. Otherwise the left function
11 * will be called with the `left` value.
12 *
13 * **Maybe**
14 *
15 * If the maybe is Some than the right function will be executed with the `some` value and the value of the function
16 * returned. Otherwise the left function with be called without an argument.
17 *
18 * **Result**
19 *
20 * If the result is Ok than the right function will be executed with the `Ok` value and the value of the function
21 * returned. Otherwise the left function will be called with the `Error` value.
22 *
23 * **Validation**
24 *
25 * If the validation is Success than the right function will be executed with the `Success` value and the value of the function
26 * returned. Otherwise the left function will be called with the `Failure` value.
27 *
28 * Supported monadic libraries: {@link https://monet.github.io/monet.js/|monet.js}, {@link https://folktale.origamitower.com/|folktale}, {@link https://github.com/ramda/ramda-fantasy|ramda-fantasy}
29 *
30 * @func cata
31 * @memberOf RA
32 * @since {@link https://char0n.github.io/ramda-adjunct/1.4.0|v1.4.0}
33 * @category Function
34 * @sig (a -> b) -> (a -> c) -> Cata a -> b | c
35 * @param {Function} leftFn The left function that consumes the left value
36 * @param {Function} rightFn The right function that consumes the right value
37 * @param {Cata} catamorphicObj Either, Maybe or any other type with catamorphic capabilities (`cata` or `either` method)
38 * @return {*}
39 * @see {@link https://monet.github.io/monet.js/#cata|cata explained}
40 * @example
41 *
42 * // Either
43 * const eitherR = Either.Right(1);
44 * const eitherL = Either.Left(2);
45 *
46 * RA.cata(identity, identity, eitherR); //=> 1
47 * RA.cata(identity, identity, eitherL); //=> 2
48 *
49 * // Maybe
50 * const maybeSome = Maybe.Some(1);
51 * const maybeNothing = Maybe.Nothing();
52 *
53 * RA.cata(identity, identity, maybeSome); //=> 1
54 * RA.cata(identity, identity, maybeNothing); //=> undefined
55 */
56var catamorphism = curry(function (leftFn, rightFn, catamorphicObj) {
57 // folktale support
58 if (isFunction(catamorphicObj.matchWith)) {
59 return catamorphicObj.matchWith({
60 // Result type
61 Ok: function Ok(_ref) {
62 var value = _ref.value;
63 return rightFn(value);
64 },
65 Error: function Error(_ref2) {
66 var value = _ref2.value;
67 return leftFn(value);
68 },
69 // Maybe type
70 Just: function Just(_ref3) {
71 var value = _ref3.value;
72 return rightFn(value);
73 },
74 Nothing: function Nothing() {
75 return leftFn(undefined);
76 },
77 // Validation type
78 Success: function Success(_ref4) {
79 var value = _ref4.value;
80 return rightFn(value);
81 },
82 Failure: function Failure(_ref5) {
83 var value = _ref5.value;
84 return leftFn(value);
85 }
86 });
87 }
88 if (isFunction(catamorphicObj.cata)) {
89 return catamorphicObj.cata(leftFn, rightFn);
90 }
91 if (isFunction(catamorphicObj.getOrElse)) {
92 var elseValue = "RA.cata".concat(Math.random());
93 var value = catamorphicObj.getOrElse(elseValue);
94 return value === elseValue ? leftFn() : rightFn(value);
95 }
96 return catamorphicObj.either(leftFn, rightFn);
97});
98export default catamorphism;
Note: See TracBrowser for help on using the repository browser.