0

Background: I want to map an array of arrays of parameters over a pure fn that takes multiple parameters. I thought currying apply with null and calling Array.map() would work.

[[x1, y1], [x2, y2], [x3, y3], ... [xn, yn]].map(curry(fn.apply, null))



var curry = function(fn, value) {
    return function (x, y) {
        return fn(value, x, y);
    };
}
var fn = (x,y) => [x, y];
var fn_c = curry(fn.apply, null)
fn_c([1,2]);


Uncaught TypeError: Function.prototype.apply was called on undefined, which is a undefined and not a function
at <anonymous>:3:20
at <anonymous>:8:5

Only the apply gets remembered in the curried function, without the fn from which apply should stem. Thus the curried function tries to run apply(x,y) instead of fn.apply(x,y), which fails of course.

How can I retrieve the apply-ed function to pass around?

Rafael Emshoff
  • 2,541
  • 1
  • 35
  • 47

1 Answers1

0

Figured it out. I forgot that functions are objects as well:

var curry = function(fn, value) {
    return function (x, y) {
        return fn(value, x, y);
    };
}
var fn = (x,y) => [x, y];
var fn_c = curry(Function.prototype.apply.bind(fn), null)
fn_c([1,2]);

... that's ugly. I'll just .map(params => fn.apply(null, params)).

Rafael Emshoff
  • 2,541
  • 1
  • 35
  • 47