-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
forEach.js
60 lines (56 loc) · 1.68 KB
/
forEach.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { applyCallback } from '../../utils/applyCallback.js'
import { forEach as forEachArray } from '../../utils/array.js'
import { factory } from '../../utils/factory.js'
const name = 'forEach'
const dependencies = ['typed']
export const createForEach = /* #__PURE__ */ factory(name, dependencies, ({ typed }) => {
/**
* Iterate over all elements of a matrix/array, and executes the given callback function.
*
* Syntax:
*
* math.forEach(x, callback)
*
* Examples:
*
* math.forEach([1, 2, 3], function(value) {
* console.log(value)
* })
* // outputs 1, 2, 3
*
* See also:
*
* filter, map, sort
*
* @param {Matrix | Array} x The matrix to iterate on.
* @param {Function} callback The callback function is invoked with three
* parameters: the value of the element, the index
* of the element, and the Matrix/array being traversed.
*/
return typed(name, {
'Array, function': _forEach,
'Matrix, function': function (x, callback) {
x.forEach(callback)
}
})
})
/**
* forEach for a multidimensional array
* @param {Array} array
* @param {Function} callback
* @private
*/
function _forEach (array, callback) {
const recurse = function (value, index) {
if (Array.isArray(value)) {
forEachArray(value, function (child, i) {
// we create a copy of the index array and append the new index value
recurse(child, index.concat(i))
})
} else {
// invoke the callback function with the right number of arguments
return applyCallback(callback, value, index, array, 'forEach')
}
}
recurse(array, [])
}