Книга: Functional Programming in JavaScript
Назад: Array.prototype.reduce()
Дальше: Summary

Honorable mentions

The , , and functions are not alone in our toolbox of helper functions. There exist many more functions that can be plugged into nearly any functional application.

Array.prototype.forEach

Essentially the non-pure version of , iterates over an array and applies a function over each item. However, it doesn't return anything. It's a cleaner way of performing a loop.

Note

Syntax:

Parameters:

  • : This function is to be performed for each value of the array. With these parameters:
    • : This parameter gives the current element being processed in the array
    • : This parameter gives the index of the current element in the array
    • : This parameter gives the array being processed
  • : This function is optional. Value is used as when executing .

Examples:

var arr = [1,2,3]; var nodes = arr.map(function(x) {   var elem = document.createElement("div");   elem.textContent = x;   return elem; });  // log the value of each item arr.forEach(function(x){console.log(x)});  // append nodes to the DOM nodes.forEach(function(x){document.body.appendChild(x)});

Array.prototype.concat

When working with arrays instead of and loops, often you will need to join multiple arrays together. Another built-in JavaScript function, , takes care of this for us. The function returns a new array and leaves the old arrays untouched. It can join as many arrays as you pass to it.

console.log([1, 2, 3].concat(['a','b','c']) // concatenate two arrays); // Output: [1, 2, 3, 'a','b','c']

The original array is untouched. It returns a new array with both arrays concatenated together. This also means that the function can be chained together.

var arr1 = [1,2,3]; var arr2 = [4,5,6]; var arr3 = [7,8,9]; var x = arr1.concat(arr2, arr3); var y = arr1.concat(arr2).concat(arr3)); var z = arr1.concat(arr2.concat(arr3))); console.log(x); console.log(y); console.log(z);

Variables , and all contain .

Array.prototype.reverse

Another native JavaScript function helps with array transformations. The function inverts an array, such that the first element is now the last and the last is now the first.

However, it does not return a new array; instead it mutates the array in place. We can do better. Here's an implementation of a pure method for reversing an array:

var invert = function(arr) {   return arr.map(function(x, i, a) {     return a[a.length - (i+1)];   }); }; var q = invert([1,2,3,4]); console.log( q );

Array.prototype.sort

Much like our , , and methods, the method takes a function that defines how the objects within an array should be sorted. But, like the function, it mutates the array in place. And that's no bueno.

arr = [200, 12, 56, 7, 344]; console.log(arr.sort(function(a,b){return a–b}) ); // arr is now: [7, 12, 56, 200, 344];

We could write a pure function that doesn't mutate the array, but sorting algorithms is the source of much grief. Significantly large arrays that need to be sorted really should be organized in data structures that are designed just for that: quickStort, mergeSort, bubbleSort, and so on.

Array.prototype.every and Array.prototype.some

The and functions are both pure and high-order functions that are methods of the object and are used to test the elements of an array against a function that must return a Boolean representing the respective input. The function returns if the function returns for every element in the array, and the function returns if some elements in the array are .

Example:

function isNumber(n) {   return !isNaN(parseFloat(n)) && isFinite(n); }  console.log([1, 2, 3, 4].every(isNumber)); // Return: true console.log([1, 2, 'a'].every(isNumber)); // Return: false console.log([1, 2, 'a'].some(isNumber)); // Return: true
Назад: Array.prototype.reduce()
Дальше: Summary

bsn
thank
Vesa Karvonen
I hope you don't mind, but I’d like to point you and your readers to my high-performance optics library for JavaScript that is in production use in multiple projects, has comprehensive support for partial optics and interactive documentation: https://calmm-js.github.io/partial.lenses/ (this takes a moment to load — be patient!)