Skip to content

Commit

Permalink
Refactor times function to use Array.from for improved readability
Browse files Browse the repository at this point in the history
  • Loading branch information
mceachen committed Nov 4, 2024
1 parent 5dfcfd8 commit 206397e
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Times.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/**
* Generates an array by running a function n times
* @param n The number of times to run the function
* @param fn The function to generate each element
* @returns An array containing the results
*/
export function times<T>(n: number, f: (idx: number) => T): T[] {
return Array(n)
.fill(undefined)
.map((_, i) => f(i))
return Array.from({ length: n }, (_, i) => f(i))
}

0 comments on commit 206397e

Please sign in to comment.