Can I map a function with more then one argument?

45
May 18, 2019, at 2:10 PM

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
Answer 1

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;
Answer 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)

Answer 3

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))
Answer 4

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));
READ ALSO
import array of objects with a module as a property returns undefined when import twice

import array of objects with a module as a property returns undefined when import twice

I'm trying to import an array of objects that has a module as a propertyThe first file that imports the object has no problem, but the second one has undefined

56
I need to make this canvas transparent but none of the solutions are working

I need to make this canvas transparent but none of the solutions are working

i tried all the juery tricks and also the css tricks but none of them worki dont really know anything about canvas but trying to solve this for a day now

40
Return Object from map() function not Array

Return Object from map() function not Array

Sorry if the title is misleading I'm not sure how to accurately describe what I am looking for

25
Post comments on Facebook via console Chrome

Post comments on Facebook via console Chrome

I found this solution post comments on facebook page via console but doesn't work at now

41