Last active
January 3, 2024 16:47
-
-
Save WaldoJeffers/905e14d03f4283599bac753f73b7716b to your computer and use it in GitHub Desktop.
JavaScript one-line compose (ES6)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args))) | |
// Usage : compose functions right to left | |
// compose(minus8, add10, multiply10)(4) === 42 | |
// | |
// The resulting function can accept as many arguments as the first function does | |
// compose(add2, multiply)(4, 10) === 42 |
thanks your elaborate
Very cool piece of code - I learned a lot from it. Thank you very much for the explanation as well!
I decided to test the expanded version, works like a charm!
const multiply = (x,y) => x*y
const add6 = (x) => x+6
const minus3 = (x) => x-3
// compose(add6, minus3, multiply)(3,10)
const result = ((...arg1) => ((...arg2) => add6(minus3(...arg2)))(multiply(...arg1)))(3,10)
console.log(result) // === 33
You could probably use reduceRight
with the additional ...args
spread operator
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@teekdens sure.
We want our result function to compose its parameter functions right to left, meaning
compose(f, g)(x)
should returnf(g(x))
.Let's see how
compose
works.Array.prototype.reduce
. Now,Array.prototype.reduce
iterates from left to right (contrary to what we want in the end), so we have to be careful whether we returnf(g)
org(f)
on each iteration. The first parameter forreduce
is the accumulator (the value returned by the previous iteration), while the second parameter is the current value. So we want to make sure on each iteration that the current function gets called before the accumulator, thus we returnf(g)
. But since we also want the end result itself to be a function, we actually return a function which accepts any number of parameters on each iteration, so we return(...args) => f(g(...args))
.Let's see what happens on the first example
compose(minus8, add10, multiply10)
f === minus8
andg === add10
, because we did not provide any initial value for the accumulator. We return the following function(...args) => minus8(add10(...args))
f === (...args) => minus8(add10(...args))
andg === multiply10
. We return(...args) => f(g(...args))
, ie.(...args) => ((...x) => minus8(add10(...x))(multiply10(...args))
. This is the final resultLet's run it on
4
.((...args)
=> ((...x) => minus8(add10(...x))(multiply10(
...args
))
)(4)
gives us((...x) => minus8(add10(...x))(multiply10(
4
)))
multiply10(
4
)
is40
,((...x) => minus8(add10(...x))(multiply10(
4
)))
gives us((...x) => minus8(add10(...x))(
40
))
((
...x
) => minus8(add10(
...x
))(
40
))
gives usminus8(add10(
40
))
add10(
40
)
equals50
,minus8(add10(
40
))
gives usminus8(
50
)
minus8(
50
)
equals42