1 | import { curry, head, slice, reduce, curryN, map } from 'ramda';
|
---|
2 |
|
---|
3 | import ap from './internal/ap';
|
---|
4 |
|
---|
5 | /**
|
---|
6 | * "lifts" a function to be the specified arity, so that it may "map over" objects that satisfy
|
---|
7 | * the fantasy land Apply spec of algebraic structures.
|
---|
8 | *
|
---|
9 | * Lifting is specific for {@link https://github.com/scalaz/scalaz|scalaz} and {@link http://www.functionaljava.org/|functional java} implementations.
|
---|
10 | * Old version of fantasy land spec were not compatible with this approach,
|
---|
11 | * but as of fantasy land 1.0.0 Apply spec also adopted this approach.
|
---|
12 | *
|
---|
13 | * This function acts as interop for ramda <= 0.23.0 and {@link https://monet.github.io/monet.js/|monet.js}.
|
---|
14 | *
|
---|
15 | * More info {@link https://github.com/fantasyland/fantasy-land/issues/50|here}.
|
---|
16 | *
|
---|
17 | * @func liftFN
|
---|
18 | * @memberOf RA
|
---|
19 | * @since {@link https://char0n.github.io/ramda-adjunct/1.2.0|v1.2.0}
|
---|
20 | * @category Function
|
---|
21 | * @sig Apply a => Number -> (a... -> a) -> (a... -> a)
|
---|
22 | * @param {number} arity The arity of the lifter function
|
---|
23 | * @param {Function} fn The function to lift into higher context
|
---|
24 | * @return {Function} The lifted function
|
---|
25 | * @see {@link http://ramdajs.com/docs/#lift|R.lift}, {@link http://ramdajs.com/docs/#ap|R.ap}
|
---|
26 | * @example
|
---|
27 | *
|
---|
28 | * const { Maybe } = require('monet');
|
---|
29 | *
|
---|
30 | * const add3 = (a, b, c) => a + b + c;
|
---|
31 | * const madd3 = RA.liftFN(3, add3);
|
---|
32 | *
|
---|
33 | * madd3(Maybe.Some(10), Maybe.Some(15), Maybe.Some(17)); //=> Maybe.Some(42)
|
---|
34 | * madd3(Maybe.Some(10), Maybe.Nothing(), Maybe.Some(17)); //=> Maybe.Nothing()
|
---|
35 | */
|
---|
36 | const liftFN = curry((arity, fn) => {
|
---|
37 | const lifted = curryN(arity, fn);
|
---|
38 | return curryN(arity, (...args) => {
|
---|
39 | const accumulator = map(lifted, head(args));
|
---|
40 | const apps = slice(1, Infinity, args);
|
---|
41 | return reduce(ap, accumulator, apps);
|
---|
42 | });
|
---|
43 | });
|
---|
44 |
|
---|
45 | export default liftFN;
|
---|