Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
JavaScript one-line compose (ES6)
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

This comment has been minimized.

Copy link

tdeekens commented Nov 17, 2017

Nice. Thanks this helped me. However I fail to understand how this applied right to left with the usage of reduce. Can you elaborate?


This comment has been minimized.

Copy link
Owner Author

WaldoJeffers commented Dec 7, 2017

@teekdens sure.
We want our result function to compose its parameter functions right to left, meaning compose(f, g)(x) should return f(g(x)).
Let's see how compose works.

  1. It casts the input functions as an array, using the ES6 array rest syntax
  2. It iterates over this array using 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 return f(g) or g(f) on each iteration. The first parameter for reduce 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 return f(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)

  1. First iteration: f === minus8 and g === add10, because we did not provide any initial value for the accumulator. We return the following function (...args) => minus8(add10(...args))
  2. Now f === (...args) => minus8(add10(...args)) and g === multiply10. We return (...args) => f(g(...args)), ie. (...args) => ((...x) => minus8(add10(...x))(multiply10(...args)). This is the final result

Let's run it on 4.

  1. ((...args) => ((...x) => minus8(add10(...x))(multiply10(...args)))(4) gives us ((...x) => minus8(add10(...x))(multiply10(4)))
  2. Since multiply10(4) is 40, ((...x) => minus8(add10(...x))(multiply10(4))) gives us ((...x) => minus8(add10(...x))(40))
  3. ((...x) => minus8(add10(...x))(40)) gives us minus8(add10(40))
  4. Because add10(40) equals 50, minus8(add10(40)) gives us minus8(50)
  5. minus8(50) equals 42

This comment has been minimized.

Copy link

sponia-joker commented Dec 18, 2017

thanks your elaborate


This comment has been minimized.

Copy link

juliobetta commented Feb 22, 2018

tenor 1


This comment has been minimized.

Copy link

drewdemarest commented Nov 9, 2018

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

This comment has been minimized.

Copy link

briancodes commented Jul 18, 2019

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
You can’t perform that action at this time.