diff --git a/README.md b/README.md index d93865803..88f0d8464 100644 --- a/README.md +++ b/README.md @@ -198,6 +198,7 @@ average(1, 2, 3); * [`chainAsync`](#chainasync) * [`compose`](#compose) +* [`composeRight`](#composeright) * [`curry`](#curry) * [`defer`](#defer) * [`functionName`](#functionname) @@ -2845,6 +2846,32 @@ multiplyAndAdd5(5, 2); // 15
[⬆ Back to top](#table-of-contents) +### composeRight + +Performs left-to-right function composition. + +Use `Array.reduce()` to perform left-to-right function composition. +The first (leftmost) function can accept one or more arguments; the remaining functions must be unary. + +```js +const composeRight = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args))); +``` + +
+Examples + +```js +const add = (x, y) => x + y; +const square = x => x * x; +const addAndSquare = composeRight(add, square); +addAndSquare(1, 2); // 9 +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### curry Curries a function. diff --git a/docs/index.html b/docs/index.html index 8c64efc76..0f12043c8 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -635,6 +635,11 @@ document.bodyconst multiply = (x, y) => x * y;
 const multiplyAndAdd5 = compose(add5, multiply);
 multiplyAndAdd5(5, 2); // 15
+

composeRight

Performs left-to-right function composition.

Use Array.reduce() to perform left-to-right function composition. The first (leftmost) function can accept one or more arguments; the remaining functions must be unary.

const composeRight = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args)));
+
const add = (x, y) => x + y;
+const square = x => x * x;
+const addAndSquare = composeRight(add, square);
+addAndSquare(1, 2); // 9
 

curry

Curries a function.

Use recursion. If the number of provided arguments (args) is sufficient, call the passed function fn. Otherwise, return a curried function fn that expects the rest of the arguments. If you want to curry a function that accepts a variable number of arguments (a variadic function, e.g. Math.min()), you can optionally pass the number of arguments to the second parameter arity.

const curry = (fn, arity = fn.length, ...args) =>
   arity <= args.length ? fn(...args) : curry.bind(null, fn, arity, ...args);
 
curry(Math.pow)(2)(10); // 1024
diff --git a/snippets/composeRight.md b/snippets/composeRight.md
index 94e77aa1e..88deb7355 100644
--- a/snippets/composeRight.md
+++ b/snippets/composeRight.md
@@ -10,7 +10,7 @@ const composeRight = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args))
 ```
 
 ```js
-const add = (x,y) => x + y;
+const add = (x, y) => x + y;
 const square = x => x * x;
 const addAndSquare = composeRight(add, square);
 addAndSquare(1, 2); // 9