Adjust base library usage of for vs reduce/map (#1151)

* Adjust base library usage of `for` vs `reduce/map`

* Additional

* Adjust
This commit is contained in:
Jaco
2021-10-31 08:27:14 +02:00
committed by GitHub
parent 7ef169585f
commit a4cefa4bfc
20 changed files with 130 additions and 87 deletions
+10 -6
View File
@@ -20,15 +20,19 @@
*/
export function arrayFlatten <T> (arrays: T[][]): T[] {
// pre-allocate based on the combined size
const output = new Array<T>(arrays.reduce((length, array) => length + array.length, 0));
let size = 0;
for (const a of arrays) {
size += a.length;
}
const output = new Array<T>(size);
let index = -1;
for (let a = 0; a < arrays.length; a++) {
const array = arrays[a];
for (const a of arrays) {
// instead of pushing, we just set the entries
for (let e = 0; e < array.length; e++) {
output[++index] = array[e];
for (let e = 0; e < a.length; e++) {
output[++index] = a[e];
}
}
+7 -1
View File
@@ -21,5 +21,11 @@ import { assert } from '../assert';
export function arrayRange (size: number, startAt = 0): number[] {
assert(size > 0, 'Expected non-zero, positive number as a range size');
return new Array(size).fill(0).map((_, i) => i + startAt);
const result = new Array<number>(size).fill(0);
for (let i = 0; i < result.length; i++) {
result[i] = i + startAt;
}
return result;
}