Ramda Adjunct 2.5.0

liftFN.js

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