Currying as per the formal definitions is given a function f(x,y) -> z curry(f) -> g(x) -> h(y) -> z. But usually it's defined as taking f and fixing the first parameter. eg curry(f,5) -> g(y), with g being g(y) = f (5,y). The second definition is actually called partial application, but up to today I didn't know this as I learned it that way in university. But what confused me today was why with the first definition and a function f(x,y,z) that curry(curry(f))(x)(y)(z) != f(x,y,z). I thought c(c(f)) should be in lisp (lambda (z) (lambda (y) (lambda (x) (...)))), baffled me until sat on the train home and figured out that my curry function was working as it correctly produced (lambda () (lambda (x) ...)).

Anyways below is my Javascript code. I didn't find a shorter or more flexible version, so imho it's the best. It supports any number of arguments thanks to aist/array-comprehension.

function curry(f) { var args1 = Array.prototype.slice.call(arguments, 1); return function () { var args2 = Array.prototype.slice.call(arguments); args2 = Array.prototype.concat.call(args1, args2); return f.apply(this, args2); }; } function realCurry(f) { return function (x) { return curry(f,x); }; }

## No comments:

Post a Comment