You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.4 KiB

4 years ago
  1. import chain from "./chain.js";
  2. import compose from "./compose.js";
  3. import map from "./map.js";
  4. /**
  5. * Returns the right-to-left Kleisli composition of the provided functions,
  6. * each of which must return a value of a type supported by [`chain`](#chain).
  7. *
  8. * `R.composeK(h, g, f)` is equivalent to `R.compose(R.chain(h), R.chain(g), f)`.
  9. *
  10. * @func
  11. * @memberOf R
  12. * @since v0.16.0
  13. * @category Function
  14. * @sig Chain m => ((y -> m z), (x -> m y), ..., (a -> m b)) -> (a -> m z)
  15. * @param {...Function} ...functions The functions to compose
  16. * @return {Function}
  17. * @see R.pipeK
  18. * @deprecated since v0.26.0
  19. * @example
  20. *
  21. * // get :: String -> Object -> Maybe *
  22. * const get = R.curry((propName, obj) => Maybe(obj[propName]))
  23. *
  24. * // getStateCode :: Maybe String -> Maybe String
  25. * const getStateCode = R.composeK(
  26. * R.compose(Maybe.of, R.toUpper),
  27. * get('state'),
  28. * get('address'),
  29. * get('user'),
  30. * );
  31. * getStateCode({"user":{"address":{"state":"ny"}}}); //=> Maybe.Just("NY")
  32. * getStateCode({}); //=> Maybe.Nothing()
  33. * @symb R.composeK(f, g, h)(a) = R.chain(f, R.chain(g, h(a)))
  34. */
  35. export default function composeK() {
  36. if (arguments.length === 0) {
  37. throw new Error('composeK requires at least one argument');
  38. }
  39. var init = Array.prototype.slice.call(arguments);
  40. var last = init.pop();
  41. return compose(compose.apply(this, map(chain, init)), last);
  42. }