diff --git a/snippets/composeRight.md b/snippets/composeRight.md new file mode 100644 index 000000000..94e77aa1e --- /dev/null +++ b/snippets/composeRight.md @@ -0,0 +1,17 @@ +### 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))); +``` + +```js +const add = (x,y) => x + y; +const square = x => x * x; +const addAndSquare = composeRight(add, square); +addAndSquare(1, 2); // 9 +``` diff --git a/tag_database b/tag_database index 10dace2a4..b8afea8ab 100644 --- a/tag_database +++ b/tag_database @@ -20,6 +20,7 @@ collectInto:adapter,function,array colorize:node,utility,string compact:array compose:function +composeRight:function copyToClipboard:browser,string,advanced countBy:array,object countOccurrences:array