Skip to content

Instantly share code, notes, and snippets.

@jdmorlan
Last active August 28, 2016 23:43
Show Gist options
  • Save jdmorlan/cc4453f2e265a490658bf2b57206a1bb to your computer and use it in GitHub Desktop.
Save jdmorlan/cc4453f2e265a490658bf2b57206a1bb to your computer and use it in GitHub Desktop.
The Compose Function
const compose = (...funcs) => {
if (funcs.length === 0) {
return (arg) => arg
}
if (funcs.length === 1) {
return funcs[0]
}
const last = funcs[funcs.length - 1]
const rest = funcs.slice(0, -1)
return (...args) => rest.reduceRight((composed, f) => f(composed), last(...args))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment