dominictarr / curry

simple curry module, with nothing *too clever*, and full test coverage
312 stars 19 forks source link

curry a function where arguments have been swapped #25

Closed ghost closed 9 years ago

ghost commented 9 years ago

I need to put an argument last so I put together a simple swap function. Could you please help me understand why this don't work?

curry = require('curry');

function swap(fn, i1, i2) {
  return function() {
    var args = Array.prototype.slice.call(arguments);
    var t = args[i1];
    args[i1] = args[i2];
    args[i2] = t;
    console.log(args); // for testing purposes
    return fn.apply(null, args);
  }
}

function sub(a,b) {return a-b}
console.log(sub(3,2));
console.log( swap(sub, 0, 1)(3,2) );
console.log( curry(sub)(3)(2) );

// Error
console.log( curry(swap(sub, 0, 1)) (3)(2) );

Any help is greatly appreciated!

ghost commented 9 years ago

I realized that curry don't work with functions where the arguments are implicit. curry.to needs to be used.