Compose Interview

node v10.24.1
version: 6.0.0
endpointsharetweet
Your goal is to implement a simple `compose` function. It takes functions as arguments and returns a higher order function composing them all together. Some notes: - We've chosen to use addition as the example because it's commutative (so you don't have to worry about left to right vs right to left). - You can assume that every function compose deals with is unary (meaning they only take one argument). - Compose is also known as flow, pipe, or the . operator in haskell :)
To start, implement a two function compose. This only needs to work with 2 functions.
var compose = () => {} // <-- Fill this in var f = x => x + 5 var g = x => x + 7 var composition = compose(f, g) composition(5) == 17
Next, write a compose that will work with 3 functions.
var compose = () => {} // <-- Fill this in var f = x => x + 5 var g = x => x + 7 var h = x => x + 10 var composition = compose(f, g, h) composition(5) == 27
For the final test, generalize compose so it's variadic (meaning it can accept any number of functions as input)
var compose = () => {} var f = x => x + 5 var g = x => x + 7 var h = x => x + 10 var fgh = compose(f, g, h) var fg = compose(f, g) fgh(5) == 27 && fg(15) == 27
Loading…

no comments

    sign in to comment