Skip to content

Commit

Permalink
Replaced recurse for deepMap and deepForEach
Browse files Browse the repository at this point in the history
  • Loading branch information
dvd101x committed Sep 15, 2024
1 parent b894877 commit 09e81f4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/function/matrix/forEach.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { simplifyCallback } from '../../utils/simplifyCallback.js'
import { factory } from '../../utils/factory.js'
import { recurse } from '../../utils/array.js'
import { deepForEach } from '../../utils/array.js'

const name = 'forEach'
const dependencies = ['typed']
Expand Down Expand Up @@ -45,5 +45,5 @@ export const createForEach = /* #__PURE__ */ factory(name, dependencies, ({ type
* @private
*/
function _forEach (array, callback) {
recurse(array, array, simplifyCallback(callback, array, name))
deepForEach(array, array, simplifyCallback(callback, array, name))
}
4 changes: 2 additions & 2 deletions src/function/matrix/map.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { simplifyCallback } from '../../utils/simplifyCallback.js'
import { arraySize, broadcastSizes, broadcastTo, get, recurse } from '../../utils/array.js'
import { arraySize, broadcastSizes, broadcastTo, get, deepMap } from '../../utils/array.js'
import { factory } from '../../utils/factory.js'

const name = 'map'
Expand Down Expand Up @@ -151,6 +151,6 @@ export const createMap = /* #__PURE__ */ factory(name, dependencies, ({ typed })
* @private
*/
function _mapArray (array, callback) {
return recurse(array, array, simplifyCallback(callback, array, name))
return deepMap(array, array, simplifyCallback(callback, array, name))
}
})
6 changes: 3 additions & 3 deletions src/type/matrix/DenseMatrix.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isArray, isBigNumber, isCollection, isIndex, isMatrix, isNumber, isString, typeOf } from '../../utils/is.js'
import { arraySize, getArrayDataType, processSizesWildcard, reshape, resize, unsqueeze, validate, validateIndex, broadcastTo, get, recurse } from '../../utils/array.js'
import { arraySize, getArrayDataType, processSizesWildcard, reshape, resize, unsqueeze, validate, validateIndex, broadcastTo, get, deepMap, deepForEach } from '../../utils/array.js'
import { format } from '../../utils/string.js'
import { isInteger } from '../../utils/number.js'
import { clone, deepStrictEqual } from '../../utils/object.js'
Expand Down Expand Up @@ -541,7 +541,7 @@ export const createDenseMatrixClass = /* #__PURE__ */ factory(name, dependencies

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

/**
Expand Down
63 changes: 62 additions & 1 deletion src/utils/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ export function get (array, index) {
* @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.
* @returns {*} The new array with each element being the result of the callback function.
*/
export function recurse (value, array, callback, callbackArgs = 3) {
export function deepMap (value, array, callback, callbackArgs = 3) {
if (callbackArgs === 3) {
return _recurseValueIndexArray(value, [])
}
Expand Down Expand Up @@ -883,6 +883,67 @@ export function recurse (value, array, callback, callbackArgs = 3) {
}
}

/**
* Recursive function to applya for each elmeent in a multi-dimensional array.
*
* @param {*} value - The current value being processed in the array.
* @param {Array} array - The array being processed.
* @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.
* @returns {*} The new array with each element being the result of the callback function.
*/
export function deepForEach (value, array, callback, callbackArgs = 3) {
switch (callbackArgs) {
case 3:
_recurseValueIndexArray(value, [])
break

case 2:
_recurseValueIndex(value, [])
break
case 1:
_recurseValue(value, callback)
break
default:
_recurseValueIndexArray(value, [])
break
}

function _recurseValue (value) {
if (isArray(value)) {
value.forEach(child => _recurseValue(child))
} else {
callback(value)
}
}

function _recurseValueIndex (value, index) {
if (isArray(value)) {
const dim = index.push(null) - 1
value.forEach((child, i) => {
index[dim] = i
_recurseValueIndex(child, index)
})
index.pop()
} else {
callback(value, [...index])
}
}

function _recurseValueIndexArray (value, index) {
if (isArray(value)) {
const dim = index.push(null) - 1
value.forEach((child, i) => {
index[dim] = i
_recurseValueIndexArray(child, index)
})
index.pop()
} else {
// invoke the callback function with the right number of arguments
callback(value, [...index], array)
}
}
}

/**
* Deep clones a multidimensional array
* @param {Array} array
Expand Down

0 comments on commit 09e81f4

Please sign in to comment.