cast-curry
A hybrid curry, and partial function.
Curry, or partially apply a function.
Example
var curry = ; { return a + b + c;} var f = ;//print xyzconsole;
If some parameters positioned more to the left aren't known yet then you can use a placeholder.
var curry = _ = ; { return a + b + c + d;} var part = ;//print wxyzconsole;
If the function you want to curry is variadic you can add a placeholder to the right if you know how many arguments you will need.
var curry = _ = ; { var str = ''; forvar i=0; i<argumentslength; i++ str += argumentsi; return str;} var getGreeting = ;//print Hello how are you?console;
Variadic functions won't work correctly if you don't add a placeholder to the right.
var getGreeting = ;//This is an error.console;
this!
You can pass a this
context as the first argument to set the function's context.
If you do then set the function to curry to the second argument.
Here push
is native. Like many native functions push
doesn't have a Function.length
property so you need to set a placeholder as the first push
parameter.
var array = ; var push = ; //print 1console; //print [ '5' ]console; //print 2console; //print [ '5', '10' ]console;
Warning!
Objects, and arrays still have references to their data even when passed to a function.
So if you curry a function with a default parameter of array, or object later they can be altered. This means sometimes you might find you're data has changed unexpectedly.
This is ok if that's what you want, and you think you can keep track of that.
In many situations you probably don't want partial parameters to change.
For partial parameters use a deep clone
function, or a library like Immutable.js to order to make your objects, or list types not changeable if you want to limit side effects.
About
Curry functions, and make partials out of functions.
The placeholder parameter concept is taken from the really cool library Rambda.