This is a small project to play around with function currying. The original code can be found here. I've just made a few changes to improve readability:
- Replaced the first if
for the logical ORES6 default parameter.
function cook(f, args=[]) {
// args = args || [];
//(...)
}
- Removed the context argument. I just wanted to curry the function without thinking about which scope I'm working with.
- You'll need node 6 to run this project.
- Function Generators and Iterators on tests FTW!
- Generators in JavaScript - What, Why and How - FunFunFunction #34
- 7 Surprising Things I Learned Writing a Fibonacci Generator in JavaScript
Function Currying is a technique where you can take a function with n arguments and transform it into a series of functions (n to be precise) with arity 1. The technique is named after his creator Haskel Curry, according to Wikipedia. Still have to confirm this with more sources. Haskell has this for free for every function. I'm still trying to figure out a use case for this technique because every use case that I thought I was using Currying, I was actually using Partial Application or just creating a clojure to return a function that has access to that scope.
The difference between partial application and Currying is that with partial application you take a function with n arguments and you transform it into a function with fewer arguments. In javascript this can be achieved, for example, with the bind operator.
As for the clojure example, the use case I had was the handling of the error function when working with Parse's javascript SDK promises. Again, in order to improve code readability, instead of writing the callback function directly as one of the arguments of the then, I wrote something like this
function handleError (res, message) {
return function (error) {
res.json(`${message} ${JSON.stringify(error)}`);
}
};
Still have to figure out a use case for Function Currying, yet in the mean time I have this small project to play around with the technique and understand a little bit more.