Skip to content

Commit 09e81f4

Browse files
committed
Replaced recurse for deepMap and deepForEach
1 parent b894877 commit 09e81f4

File tree

4 files changed

+69
-8
lines changed

4 files changed

+69
-8
lines changed

src/function/matrix/forEach.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { simplifyCallback } from '../../utils/simplifyCallback.js'
22
import { factory } from '../../utils/factory.js'
3-
import { recurse } from '../../utils/array.js'
3+
import { deepForEach } from '../../utils/array.js'
44

55
const name = 'forEach'
66
const dependencies = ['typed']
@@ -45,5 +45,5 @@ export const createForEach = /* #__PURE__ */ factory(name, dependencies, ({ type
4545
* @private
4646
*/
4747
function _forEach (array, callback) {
48-
recurse(array, array, simplifyCallback(callback, array, name))
48+
deepForEach(array, array, simplifyCallback(callback, array, name))
4949
}

src/function/matrix/map.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { simplifyCallback } from '../../utils/simplifyCallback.js'
2-
import { arraySize, broadcastSizes, broadcastTo, get, recurse } from '../../utils/array.js'
2+
import { arraySize, broadcastSizes, broadcastTo, get, deepMap } from '../../utils/array.js'
33
import { factory } from '../../utils/factory.js'
44

55
const name = 'map'
@@ -151,6 +151,6 @@ export const createMap = /* #__PURE__ */ factory(name, dependencies, ({ typed })
151151
* @private
152152
*/
153153
function _mapArray (array, callback) {
154-
return recurse(array, array, simplifyCallback(callback, array, name))
154+
return deepMap(array, array, simplifyCallback(callback, array, name))
155155
}
156156
})

src/type/matrix/DenseMatrix.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isArray, isBigNumber, isCollection, isIndex, isMatrix, isNumber, isString, typeOf } from '../../utils/is.js'
2-
import { arraySize, getArrayDataType, processSizesWildcard, reshape, resize, unsqueeze, validate, validateIndex, broadcastTo, get, recurse } from '../../utils/array.js'
2+
import { arraySize, getArrayDataType, processSizesWildcard, reshape, resize, unsqueeze, validate, validateIndex, broadcastTo, get, deepMap, deepForEach } from '../../utils/array.js'
33
import { format } from '../../utils/string.js'
44
import { isInteger } from '../../utils/number.js'
55
import { clone, deepStrictEqual } from '../../utils/object.js'
@@ -541,7 +541,7 @@ export const createDenseMatrixClass = /* #__PURE__ */ factory(name, dependencies
541541

542542
// determine the new datatype when the original matrix has datatype defined
543543
// TODO: should be done in matrix constructor instead
544-
const data = recurse(me._data, me, simpleCallback, numberOfArguments)
544+
const data = deepMap(me._data, me, simpleCallback, numberOfArguments)
545545
const datatype = me._datatype !== undefined
546546
? getArrayDataType(data, typeOf)
547547
: undefined
@@ -559,7 +559,7 @@ export const createDenseMatrixClass = /* #__PURE__ */ factory(name, dependencies
559559
// matrix instance
560560
const me = this
561561
const simpleCallback = simplifyCallback(callback, me._data, 'forEach')
562-
recurse(this._data, me, simpleCallback, numberOfArguments)
562+
deepForEach(this._data, me, simpleCallback, numberOfArguments)
563563
}
564564

565565
/**

src/utils/array.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ export function get (array, index) {
833833
* @param {Function} callback - Function that produces the element of the new Array, taking three arguments: the value of the element, the index of the element, and the Array being processed.
834834
* @returns {*} The new array with each element being the result of the callback function.
835835
*/
836-
export function recurse (value, array, callback, callbackArgs = 3) {
836+
export function deepMap (value, array, callback, callbackArgs = 3) {
837837
if (callbackArgs === 3) {
838838
return _recurseValueIndexArray(value, [])
839839
}
@@ -883,6 +883,67 @@ export function recurse (value, array, callback, callbackArgs = 3) {
883883
}
884884
}
885885

886+
/**
887+
* Recursive function to applya for each elmeent in a multi-dimensional array.
888+
*
889+
* @param {*} value - The current value being processed in the array.
890+
* @param {Array} array - The array being processed.
891+
* @param {Function} callback - Function that produces the element of the new Array, taking three arguments: the value of the element, the index of the element, and the Array being processed.
892+
* @returns {*} The new array with each element being the result of the callback function.
893+
*/
894+
export function deepForEach (value, array, callback, callbackArgs = 3) {
895+
switch (callbackArgs) {
896+
case 3:
897+
_recurseValueIndexArray(value, [])
898+
break
899+
900+
case 2:
901+
_recurseValueIndex(value, [])
902+
break
903+
case 1:
904+
_recurseValue(value, callback)
905+
break
906+
default:
907+
_recurseValueIndexArray(value, [])
908+
break
909+
}
910+
911+
function _recurseValue (value) {
912+
if (isArray(value)) {
913+
value.forEach(child => _recurseValue(child))
914+
} else {
915+
callback(value)
916+
}
917+
}
918+
919+
function _recurseValueIndex (value, index) {
920+
if (isArray(value)) {
921+
const dim = index.push(null) - 1
922+
value.forEach((child, i) => {
923+
index[dim] = i
924+
_recurseValueIndex(child, index)
925+
})
926+
index.pop()
927+
} else {
928+
callback(value, [...index])
929+
}
930+
}
931+
932+
function _recurseValueIndexArray (value, index) {
933+
if (isArray(value)) {
934+
const dim = index.push(null) - 1
935+
value.forEach((child, i) => {
936+
index[dim] = i
937+
_recurseValueIndexArray(child, index)
938+
})
939+
index.pop()
940+
} else {
941+
// invoke the callback function with the right number of arguments
942+
callback(value, [...index], array)
943+
}
944+
}
945+
}
946+
886947
/**
887948
* Deep clones a multidimensional array
888949
* @param {Array} array

0 commit comments

Comments
 (0)