We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 464b80b commit 0df993dCopy full SHA for 0df993d
Function Composition
@@ -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
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