Skip to content

Commit

Permalink
refactor: simplify reduce implementation slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Jan 26, 2025
1 parent 7a14bf6 commit a280dd5
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/async/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,17 @@ export async function reduce<T, K>(
if (!array) {
array = []
}
const indices = array.keys()
let index = 0
let acc = initialValue
// biome-ignore lint/style/noArguments:
if (acc === undefined && arguments.length < 3) {
if (!array.length) {
throw new TypeError('Reduce of empty array with no initial value')
}
acc = array[0] as any
indices.next()
acc = array[index++] as any
}
for (const index of indices) {
acc = await reducer(acc!, array[index], index)
while (index < array.length) {
acc = await reducer(acc!, array[index], index++)
}
return acc!
}

0 comments on commit a280dd5

Please sign in to comment.