Question

Eloquent Javascript contains the following code examples:

function map(func, array) {
  var result = [];
  forEach(array, function (element) {
    result.push(func(element));
  });
  return result;
}

function asArray(quasiArray, start) {
  var result = [];
  for (var i = (start || 0); i < quasiArray.length; i++)
    result.push(quasiArray[i]);
  return result;
}

function partial(func) {
  var fixedArgs = asArray(arguments, 1);
  return function(){
    return func.apply(null, fixedArgs.concat(asArray(arguments)));
  };
}

function square(x) {return x * x;}

console.log(map(partial(map, square), [[10, 100], [12, 16], [0, 1]]));

I understand map and asArray, but partial is confusing to me. The above call returns

[[100, 10000], [144, 256], [0, 1]]

but I don't understand how. Here are my questions:

1) In the partial definition, is the arguments variable in the first line the same as the arguments variable in the last line? If not, what are the sources for each of those arguments objects?

2) When the function call occurs on the final code line above, my understanding is that fixedArgs is bound to [square] (that is, an array containing the square function). Then concat is called on it with the 2-dimensional array. Many problems, and not the correct answer, arise from these bindings, so they can't be the correct ones. How are the variables in partial bound when it's called?

Was it helpful?

Solution

1) No, they're different. arguments in the first line contains the arguments to partial. arguments in the last line contains the arguments to the anonymous function that partial returned.

2) fixedArgs contains the array [square]. Since it was created from asArray(arguments, 1), it skips argument 0, which is map, and contains all the remaining arguments.

OTHER TIPS

1) In the partial definition, is the arguments variable in the first line the same as the arguments variable in the last line? If not, what are the sources for each of those arguments objects?

No it's not. arguments is based on the current function. In the first line it's the arguments supplied to partial itself, in the third one it's the arguments applied to the function.

To quote the specification:

When control enters an execution context for function code, an arguments object is created

What we're doing here is applying the arguments supplied initially in addition to the ones supplied later on. We're concating the arguments given initially with the ones given during invocation to create a partial.

2) When the function call occurs on the final code line above, my understanding is that fixedArgs bound to [map] (that is, an array containing the map function). Then concat is called on it with the 2-dimensional array. Many problems, and not the correct answer, arise from these bindings, so they can't be the correct ones. How are the variables in partial bound when it's called?

What the code in the last line is doing is:

  • partial(map, square) - creates a function out of map that uses square as the first argument, this basically creates a function that gets an array and squares it.

  • map(result, [[10, 100], [12, 16], [0, 1]]) - this performs a mapping again. This time we map every element in the array to the function declared above. The elements in the array are arrays themselves and the function we defined above takes an array and squares all its elements so what it does is square all the elements of the internal arrays.

So we get expect to get [100,1000],...

1) arguments is a special JavaScript variable. It is an array of all arguments passed to that function. So those arguments are two different variables: The first one refers to the arguments passed to partial, the second one to those passed to the returned function.

2) The partial function returns a new function that, when invoked, calls map with square as the only argument. The outer map call processes all arrays in the given (two-dimensional) array: map processes each part, partial(map, square) prepares a function that squares all values in the passed array.

Edit: I am definitely typing too slowly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top