Skip to content

Commit 0df993d

Browse files
authored
Create Function Composition
1 parent 464b80b commit 0df993d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Function Composition

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {Function[]} functions
3+
* @return {Function}
4+
*/
5+
var compose = function(functions) {
6+
if( functions.length === 0) {
7+
return function(x) {
8+
return x;
9+
};
10+
}
11+
return functions.reduceRight(function(prevFn, nextFn) {
12+
return function(x) {
13+
return nextFn(prevFn(x));
14+
};
15+
});
16+
};
17+
18+
/**
19+
* const fn = compose([x => x + 1, x => 2 * x])
20+
* fn(4) // 9
21+
*/

0 commit comments

Comments
 (0)