Allow One Function Call
A once wrapper: the first call runs, every call after it does nothing.
JavaScript · ES6 — Pratik Rai ·
Fold an array of functions into one, applied right to left.
Write compose(functions) returning a single function f where f(x) applies each function from right to left.
compose([f, g, h])(x) is f(g(h(x))). An empty array gives a function that returns its input unchanged.
Input:
JSfile.javascript1const fn = compose([(x) => x + 1, (x) => x * 2]); 2fn(4);
Output:
9
The rightmost function runs first: 4 doubled is 8, then incremented is 9.
pipe, which many codebases prefer because it reads in execution order.Goal: Return one function that applies the whole array right to left.
Continue learning with these related challenges
A once wrapper: the first call runs, every call after it does nothing.
JavaScript · ES6 — Pratik Rai ·
Convert a Node-style (error, value) callback API into one that returns a promise.
JavaScript · ES6 — Pratik Rai ·
Cache a function by its arguments so the same call never computes twice.
JavaScript · ES6 — Pratik Rai ·
A once wrapper: the first call runs, every call after it does nothing.
JavaScript · ES6
Pratik Rai ·
Convert a Node-style (error, value) callback API into one that returns a promise.
JavaScript · ES6
Pratik Rai ·
Cache a function by its arguments so the same call never computes twice.
JavaScript · ES6
Pratik Rai ·