[d24f17c] | 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 | * @symb R.compose(f, g, h)(a)(b) = f(g(h(a)))(b)
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | export default function compose() {
|
---|
| 30 | if (arguments.length === 0) {
|
---|
| 31 | throw new Error('compose requires at least one argument');
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | return pipe.apply(this, reverse(arguments));
|
---|
| 35 | } |
---|