Chapter 5. Functional Programming

 

1.  Functions that operate on other functions are called higher-order functions. By manipulating functions, they can talk about actions on a new level.

 

2.  Functions have a method called apply, it takes two arguments. The second argument is an array containing the arguments to which a function must be applied.

 

3.  forEach function:

function forEach(array, action) {
  for (var i = 0; i < array.length; i++)
    action(array[i]);
}

See Array.prototype.forEach 

 

4.  reduce function combines an array into a single value by repeatedly using a function that combines an element of the array with a base value:

function reduce(combine, base, array) {
  forEach(array, function (element) {
    base = combine(base, element);
  });
  return base;
}

 See Array.prototype.reduce

 

5.  map function goes over an array, applying a function to every element, just like forEach. But instead of discarding the values returned by the function, it builds up a new array from these values:

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

See Array.prototype.map 

 

6.  concat method on arrays creates a new array that concatenates the argument it is given with the array on which it is called. Calling concat is wasteful, because it creates a whole new array every time. 

 

7.  String.indexOf returns -1 when it doesn’t find its substring. The replace method of strings creates a new string in which all occurrences of the pattern in the first argument are replaced by the second argument.

 

8.  Creating new strings, especially big strings, is quite a bit of work for the computer. Remember that JavaScript string values never change. If you concatenate something to them, a new string is created, and the old ones stay intact. If we build up a big string by concatenating lots of little strings, new strings have to be created at every step, only to be thrown away when the next piece is concatenated to them. If, on the other hand, we store all the little strings in an array and then join them, only one big string has to be created.

 

9.  In HTML, anchors are created with the a tag, just like links. To make it an anchor instead of a link, the tag gets a name attribute, rather than of an href attribute.

 

10.  We may want something called partial application which take a function X and one or more arguments and then create a new function that calls X with both the original arguments and any newly passed ones:

function partial(func) {
  var knownArgs = arguments;
  return function() {
    var realArgs = [];
    for (var i = 1; i < knownArgs.length; i++)
      realArgs.push(knownArgs[i]);
    for (var i = 0; i < arguments.length; i++)
      realArgs.push(arguments[i]);
    return func.apply(null, realArgs);
  };
}

 

11.  The reason map takes its function argument before its array argument is that it is often useful to partially apply map by giving it a function. This “lifts” the function from operating on a single value to operating on an array of values:

map(partial(map, square), [[10], [0, 2], [3]]);

→ [[100], [0, 4], [9]]

 

12.The sum function can now simply be written like this: var sum = partial(reduce, op["+"], 0);

你可能感兴趣的:(map,foreach,reduce,partial)