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.

33 lines
1.1 KiB

4 years ago
  1. import pipe from "./pipe.js";
  2. import reverse from "./reverse.js";
  3. /**
  4. * Performs right-to-left function composition. The last argument may have
  5. * any arity; the remaining arguments must be unary.
  6. *
  7. * **Note:** The result of compose is not automatically curried.
  8. *
  9. * @func
  10. * @memberOf R
  11. * @since v0.1.0
  12. * @category Function
  13. * @sig ((y -> z), (x -> y), ..., (o -> p), ((a, b, ..., n) -> o)) -> ((a, b, ..., n) -> z)
  14. * @param {...Function} ...functions The functions to compose
  15. * @return {Function}
  16. * @see R.pipe
  17. * @example
  18. *
  19. * const classyGreeting = (firstName, lastName) => "The name's " + lastName + ", " + firstName + " " + lastName
  20. * const yellGreeting = R.compose(R.toUpper, classyGreeting);
  21. * yellGreeting('James', 'Bond'); //=> "THE NAME'S BOND, JAMES BOND"
  22. *
  23. * R.compose(Math.abs, R.add(1), R.multiply(2))(-4) //=> 7
  24. *
  25. * @symb R.compose(f, g, h)(a, b) = f(g(h(a, b)))
  26. */
  27. export default function compose() {
  28. if (arguments.length === 0) {
  29. throw new Error('compose requires at least one argument');
  30. }
  31. return pipe.apply(this, reverse(arguments));
  32. }