Skip to content

Instantly share code, notes, and snippets.

@stratedge
Last active April 20, 2018 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stratedge/eedabc9881cfb58deb2cad6ba2dd3e58 to your computer and use it in GitHub Desktop.
Save stratedge/eedabc9881cfb58deb2cad6ba2dd3e58 to your computer and use it in GitHub Desktop.
JS Curry Demo
/**
* Curry function that will prepend given arguments to the provided function
* before being run.
*
* (Technically this isn't true curry'ing, but it makes enough sense to call it that here for now)
*
* @param {[Function]} func
* @param {...[mixed]} args
* @return {[Function]}
*/
function curry(func, ...args) {
return function () {
func.apply(null, arr.concat(Array.from(arguments)))
}
}
const client = 'Client Object'
const otherThing = 'Other Thing'
// Create our function handler that will be given the client first
const handler = function (client, otherThing) {
console.log(client)
console.log(otherThing)
}
// Use the curry method to create a new function that injects the client as the first param when run
const newHandler = curry(handler, client)
// Call the handler
newHandler(otherThing) // Should log 'Client Object' and then 'Other Thing', even though at call-time we only passed the otherThing variable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment