Last active
April 20, 2018 15:41
-
-
Save stratedge/eedabc9881cfb58deb2cad6ba2dd3e58 to your computer and use it in GitHub Desktop.
JS Curry Demo
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
/** | |
* 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