UNPKG

1.49 kBJavaScriptView Raw
1var _concat =
2/*#__PURE__*/
3require("./internal/_concat");
4
5var _curry2 =
6/*#__PURE__*/
7require("./internal/_curry2");
8
9var _reduce =
10/*#__PURE__*/
11require("./internal/_reduce");
12
13var map =
14/*#__PURE__*/
15require("./map");
16/**
17 * ap applies a list of functions to a list of values.
18 *
19 * Dispatches to the `ap` method of the second argument, if present. Also
20 * treats curried functions as applicatives.
21 *
22 * @func
23 * @memberOf R
24 * @since v0.3.0
25 * @category Function
26 * @sig [a -> b] -> [a] -> [b]
27 * @sig Apply f => f (a -> b) -> f a -> f b
28 * @sig (r -> a -> b) -> (r -> a) -> (r -> b)
29 * @param {*} applyF
30 * @param {*} applyX
31 * @return {*}
32 * @example
33 *
34 * R.ap([R.multiply(2), R.add(3)], [1,2,3]); //=> [2, 4, 6, 4, 5, 6]
35 * R.ap([R.concat('tasty '), R.toUpper], ['pizza', 'salad']); //=> ["tasty pizza", "tasty salad", "PIZZA", "SALAD"]
36 *
37 * // R.ap can also be used as S combinator
38 * // when only two functions are passed
39 * R.ap(R.concat, R.toUpper)('Ramda') //=> 'RamdaRAMDA'
40 * @symb R.ap([f, g], [a, b]) = [f(a), f(b), g(a), g(b)]
41 */
42
43
44var ap =
45/*#__PURE__*/
46_curry2(function ap(applyF, applyX) {
47 return typeof applyX['fantasy-land/ap'] === 'function' ? applyX['fantasy-land/ap'](applyF) : typeof applyF.ap === 'function' ? applyF.ap(applyX) : typeof applyF === 'function' ? function (x) {
48 return applyF(x)(applyX(x));
49 } : _reduce(function (acc, f) {
50 return _concat(acc, map(f, applyX));
51 }, [], applyF);
52});
53
54module.exports = ap;
\No newline at end of file