JSFiddle - React, Tailwind, and code Playground

by Jon-Carlos Rivera

HTML

<script src="http://jon-carlos.com/scripts/masala.js"></script>

JavaScript

var slice = Array.prototype.slice,
    call = Function.prototype.call;

function toArray() {
  return call.apply(slice, arguments);
}

var spicyCurry = function(fn) {
  var filled = toArray(arguments, 1); // like doing arguments.slice(1)
  return makeCurry(fn, filled);
}

var makeCurry = function(fn, filled) {
  return function() {
    var args = toArray(arguments);
    var nfilled = filled.concat(args);

    var retFn = makeCurry(fn, nfilled);
    if (nfilled.length >= fn.length) {
      var value = fn.apply(this, nfilled.slice(-fn.length));
      retFn.valueOf = function(){ return value; }
    }
    return retFn;
  }
}

// This function only takes three arguments so that the examples below
// better illustrates what the _curry_ function is doing.
var add = spicyCurry(function(a,b,c) {
  return a + b + c;
});

// These should each print out 60.
console.log(add(10, 20, 30));
console.log(add(10, 20)(30));
console.log(add(10)(20, 30));
console.log(add(10)(20)(30));

// These should each print out 90.
console.log(add(10, 20, 30, 40));
console.log(add(10, 20)(30, 40));
console.log(add(10)(20, 30, 40));
console.log(add(10)(20)(30)(40));