Passing two arguments to the .map callback function

- 1 answer

Ad

R stands for the Ramda.js library, similar to the Underscore.js

var test = [
  {p1: 1}
];

var get = R.curry(function(prop, obj) { return obj[prop]; });
console.log(R.map(get('p1'), test));

what i know about the R.map callback that it takes only one argument but here we passed two arguments prop, obj and it's working ?

we didn't use obj so it should be undefined --> return undefined[prop] right ?

this code prove that map callback takes only one argument

var test = [
  {p1: 1}
];

function fn(arg1, arg2) {
  return arg1 + ' ' + arg2 + '\n';
}

console.log(R.map(fn, test));

the result is

["[object Object] undefined"]
Ad

Answer

Ad

This works because get is a curried function. This means that get('p1') returns a function which takes one argument. In this case, it returns a function like this:

function(obj) { return obj['p1']; });

This allows it to be passed to the R.map function.

To understand currying, the wikipedia article is rather stellar: https://en.wikipedia.org/wiki/Currying

Ad
source: stackoverflow.com
Ad