problems(js): add “2629. Function Composition”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
0c23faf222
commit
f7cdba6935
1 changed files with 17 additions and 0 deletions
17
problems/function-composition.js
Normal file
17
problems/function-composition.js
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
/**
|
||||||
|
* @param {Function[]} functions
|
||||||
|
* @return {Function}
|
||||||
|
*/
|
||||||
|
var compose = function(functions) {
|
||||||
|
return function(x) {
|
||||||
|
return functions.reduceRight(
|
||||||
|
(val, f) => f(val),
|
||||||
|
x,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* const fn = compose([x => x + 1, x => 2 * x])
|
||||||
|
* fn(4) // 9
|
||||||
|
*/
|
Loading…
Reference in a new issue