ap.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import _concat from "./internal/_concat.js";
  2. import _curry2 from "./internal/_curry2.js";
  3. import _reduce from "./internal/_reduce.js";
  4. import map from "./map.js";
  5. /**
  6. * ap applies a list of functions to a list of values.
  7. *
  8. * Dispatches to the `ap` method of the first argument, if present. Also
  9. * treats curried functions as applicatives.
  10. *
  11. * @func
  12. * @memberOf R
  13. * @since v0.3.0
  14. * @category Function
  15. * @sig [a -> b] -> [a] -> [b]
  16. * @sig Apply f => f (a -> b) -> f a -> f b
  17. * @sig (r -> a -> b) -> (r -> a) -> (r -> b)
  18. * @param {*} applyF
  19. * @param {*} applyX
  20. * @return {*}
  21. * @example
  22. *
  23. * R.ap([R.multiply(2), R.add(3)], [1,2,3]); //=> [2, 4, 6, 4, 5, 6]
  24. * R.ap([R.concat('tasty '), R.toUpper], ['pizza', 'salad']); //=> ["tasty pizza", "tasty salad", "PIZZA", "SALAD"]
  25. *
  26. * // R.ap can also be used as S combinator
  27. * // when only two functions are passed
  28. * R.ap(R.concat, R.toUpper)('Ramda') //=> 'RamdaRAMDA'
  29. * @symb R.ap([f, g], [a, b]) = [f(a), f(b), g(a), g(b)]
  30. */
  31. var ap =
  32. /*#__PURE__*/
  33. _curry2(function ap(applyF, applyX) {
  34. return typeof applyX['fantasy-land/ap'] === 'function' ? applyX['fantasy-land/ap'](applyF) : typeof applyF.ap === 'function' ? applyF.ap(applyX) : typeof applyF === 'function' ? function (x) {
  35. return applyF(x)(applyX(x));
  36. } : _reduce(function (acc, f) {
  37. return _concat(acc, map(f, applyX));
  38. }, [], applyF);
  39. });
  40. export default ap;