Skip to content

Instantly share code, notes, and snippets.

@diablero13
Last active January 19, 2018 04:06
Show Gist options
  • Save diablero13/9e063e652d3730109f47f286ef97a167 to your computer and use it in GitHub Desktop.
Save diablero13/9e063e652d3730109f47f286ef97a167 to your computer and use it in GitHub Desktop.
JavaScript Compose Function
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
// Alternative version
// const compose = (...fns) => x => fns.reduce((v, fn) => fn(v), x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment