3

In EE (Google Earth Engine Javascript API) I can do

var listOfNumbers = [0, 1, 1, 2, 3, 5];
print('List of numbers:', listOfNumbers);

var add_ten = function(n) {
  var m = n + 10;
  return m;
}

var listOfNumbers_ = listOfNumbers.map(add_ten);
print('List of numbers:', listOfNumbers_);

What if I want to add x (or another value) instead of 10? Like

var listOfNumbers = [0, 1, 1, 2, 3, 5];
print('List of numbers:', listOfNumbers);

var add_x = function(n, x) {
  var m = n + x;
  return m;
}

var listOfNumbers_ = listOfNumbers.map(add_x);
print('List of numbers:', listOfNumbers_);

How do I pass that x?

I tried

var listOfNumbers_ = listOfNumbers.map(add_x(100));
print('List of numbers:', listOfNumbers_);

But got NaN is not a function.

Also tried

var listOfNumbers_ = listOfNumbers.map(add_x, 100);
print('List of numbers:', listOfNumbers_);

Then got the following interesting result (which I don't understand)

0,2,3,5,7,10
KcFnMi
  • 5,516
  • 10
  • 62
  • 136

4 Answers4

4

If you don't want to change your current function, then you can use partial application via Function#bind to supply one argument to it but not the other:

var listOfNumbers = [0, 1, 1, 2, 3, 5];
var add_x = function(n, x) {
  var m = n + x;
  return m;
}

var add_10 = add_x.bind(null, 10);

var listOfNumbers_ = listOfNumbers.map(add_10);

console.log(listOfNumbers_);

Or even even:

var listOfNumbers_ = listOfNumbers.map(add_x.bind(null, 10));

Alternatively, you can use currying:

var listOfNumbers = [0, 1, 1, 2, 3, 5];
var add_x = function(n) {
  return function (x){
    var m = n + x;
    return m;
  }
}

var add_10 = add_x(10);

var listOfNumbers_ = listOfNumbers.map(add_10);

console.log(listOfNumbers_);

Or even:

var listOfNumbers_ = listOfNumbers.map(add_x(10));

You can shorten the curried definition using arrow functions:

var add_x = n => x => n+x;
VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • I'm taking the alternative using currying and without arrow function syntax (I didn't figured out how to use it on EE). – KcFnMi May 18 '19 at 07:28
2

Here is a method using currying:

var listOfNumbers = [0, 1, 1, 2, 3, 5];
console.log('List of numbers:', listOfNumbers);

var add_x = (n) => (x) => {
  return n + x;
}

var listOfNumbers_ = listOfNumbers.map(add_x(100));
console.log('List of numbers:', listOfNumbers_);

Note the add_x variable is set to a function that is called with the second value as the parameter.

When used with the .map() method, the first value comes from the array and the second is provided as the parameter to the function. It is the equivalent of calling it like this:

add_x(1)(100)

Randy Casburn
  • 13,840
  • 1
  • 16
  • 31
2

The array sum makes perfect sense when you look at the function signature of Array#map, as the first two parameters provided to the callback function are the element and the index iterated.

You have at least two choices:

  1. Function#bind, to prefix the initial argument n and let x be the element from the array,
  2. Anonymous function expression:

E.g.

const arr = [0, 1, 1, 2, 3, 5];
const r1 = arr.map(add_x.bind(null, n));
const r2 = arr.map(x => add_x(n, x));
tehhowch
  • 9,645
  • 4
  • 24
  • 42
1

You can simply write a new function and pass it to the .map() method:

function mapFn(value) { return add_x(value, 100) }
listOfNumbers.map(mapFn)

You can use arrow function syntax to make it even simpler:

listOfNumbers.map(value => add_x(value, 100))
R Xy
  • 197
  • 6