Skip to content

Commit

Permalink
refactor deepMap to simplify zero-skipping logic and utilize iterableMap
Browse files Browse the repository at this point in the history
  • Loading branch information
dvd101x committed Feb 3, 2025
1 parent a0e7ca5 commit 4a507e3
Showing 1 changed file with 5 additions and 22 deletions.
27 changes: 5 additions & 22 deletions src/utils/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isCollection, isMatrix } from './is.js'
import { IndexError } from '../error/IndexError.js'
import { arraySize, findFirst as arrayFindFirst } from './array.js'
import { _switch } from './switch.js'
import { forEach as iterableForEach } from './iterable.js'
import { forEach as iterableForEach, map as iterableMap } from './iterable.js'

/**
* Test whether an array contains collections
Expand Down Expand Up @@ -56,28 +56,11 @@ export function deepForEach (array, callback) {
* @return {Array | Matrix} res
*/
export function deepMap (array, callback, skipZeros) {
if (skipZeros) {
const callbackSkip = (x) => x === 0 ? 0 : callback(x)
if (isMatrix(array)) {
return array.create(recurse(array.valueOf(), callbackSkip), array.datatype())
} else {
return recurse(array, callbackSkip)
}
const callbackSkip = skipZeros ? x => x === 0 ? 0 : callback(x) : x => callback(x)
if (isMatrix(array)) {
return array.map(callbackSkip)
} else {
if (isMatrix(array)) {
return array.create(recurse(array.valueOf(), callback), array.datatype())
} else {
return recurse(array, callback)
}
}
function recurse (array) {
if (Array.isArray(array)) {
return array.map(function (x) {
return recurse(x)
})
} else {
return callback(array)
}
return iterableMap(array, callbackSkip, false, false, array)
}
}

Expand Down

0 comments on commit 4a507e3

Please sign in to comment.