A curry function without anything too clever(... because hunger is the finest spice)
Why
If you don't know currying, or aren't sold on it's awesomeness, perhaps a friendly blog post will help.
API
curry
var curry = require('curry');//-- creating a curried function is pretty//-- straight forward:var add = curry(function(a, b){ return a + b });//-- it can be called like normal:add(1, 2) //= 3//-- or, if you miss off any arguments,//-- a new funtion that expects all (or some) of//-- the remaining arguments will be created:var add1 = add(1);add1(2) //= 3;//-- curry knows how many arguments a function should take//-- by the number of parameters in the parameter list//-- in this case, a function and two arrays is expected
curry.to
Sometimes it's necessary (especially when wrapping variadic functions) to explicitly provide an arity for your curried function:
var sum = function(){ var nums = [].slice.call(arguments); return nums.reduce(function(a, b){ return a + b });}var sum3 = curry.to(3, sum);var sum4 = curry.to(4, sum);sum3(1, 2)(3) //= 6sum4(1)(2)(3, 4) //= 10
curry.adapt
It's a (sad?) fact that JavaScript functions are often written to take the 'context' object as the first argument.
With curried functions, of course, we want it to be the last object. curry.adapt shifts the context to the last argument,
to give us a hand with this:
define(['libs/curry.min'], function(curry){ //-- assuming libs/curry.min.js is the downloaded minified version from this repo, //-- curry will be available here});
browser
If you're not using tools like browserify or require.js, you can load curry globally:
<script src="libs/curry.min.js"></script><script> <!-- curry available here --></script>
∏∏
//-- (fn, a, b). zipWith will combine two arrays using a function: