Kevin Meredith
Kevin Meredith

Reputation: 41909

Using Curry Function in JS

Working on Secrets of the JavaScript Ninja, I saw the curry function.

Function.prototype.curry = function() {
    var fn = this, args = Array.prototype.slice.call(arguments);
    return function() {
      return fn.apply(this, args.concat(
        Array.prototype.slice.call(arguments)));
    };
  };

Then I tried to use it by currying the split function (which inherited it through the Function.prototype.curry definition.

var splitIt = String.prototype.split.curry(/,\s*/); // split string into array
var results = splitIt("Mugan, Jin, Fuu");
console.log("results", results);    

But [] prints out for the results. Why?

http://jsfiddle.net/2KCP8/

Upvotes: 4

Views: 764

Answers (1)

Pointy
Pointy

Reputation: 413709

Your "splitIt" function still expects that this will refer to the string to be split. You haven't arranged for that to be the case here.

Try

var results = splitIt.call("Mugan, Jin, Fuu");

Upvotes: 3

Related Questions