diff --git a/src/generics/lib.generics.ts b/src/generics/lib.generics.ts index 00bd499..6bca4ac 100644 --- a/src/generics/lib.generics.ts +++ b/src/generics/lib.generics.ts @@ -116,8 +116,10 @@ namespace __Array { return this.join(); } - function every(this: T[], func: (v: T) => boolean) { - for (const v of this) if (!func(v)) return false; + // A callback takes the element and its index, as JavaScript gives them; one that takes the element only + // is given as one taking both (the compiler's fewer-parameters thunk). + function every(this: T[], func: (v: T, i: number) => boolean) { + for (let i = 0; i < this.length; i++) if (!func(this[i], i)) return false; return true; } @@ -150,47 +152,46 @@ namespace __Array { return newArray; } - function filter(this: T[], func: (v: T) => boolean) { + function filter(this: T[], func: (v: T, i: number) => boolean) { let result: T[] = []; - for (const v of this) if (func(v)) result.push(v); + for (let i = 0; i < this.length; i++) if (func(this[i], i)) result.push(this[i]); return result; } - function* filter2(this: T[], func: (v: T) => boolean) { - for (const v of this) if (func(v)) yield v; + function* filter2(this: T[], func: (v: T, i: number) => boolean) { + for (let i = 0; i < this.length; i++) if (func(this[i], i)) yield this[i]; } - function find(this: T[], func: (v: T) => boolean): T | undefined { - for (const v of this) if (func(v)) return v; + function find(this: T[], func: (v: T, i: number) => boolean): T | undefined { + for (let i = 0; i < this.length; i++) if (func(this[i], i)) return this[i]; return undefined; } - function findIndex(this: T[], func: (v: T) => boolean) { - let i = -1; - for (const v of this) if (i++, func(v)) return i; + function findIndex(this: T[], func: (v: T, i: number) => boolean) { + for (let i = 0; i < this.length; i++) if (func(this[i], i)) return i; return -1; } - function findLast(this: T[], func: (v: T) => boolean) { + function findLast(this: T[], func: (v: T, i: number) => boolean) { for (let i = this.length - 1; i >= 0; i--) { const v = this[i]; - if (func(v)) return v; + if (func(v, i)) return v; } return -1; } - function findLastIndex(this: T[], func: (v: T) => boolean) { + function findLastIndex(this: T[], func: (v: T, i: number) => boolean) { for (let i = this.length - 1; i >= 0; i--) { const v = this[i]; - if (func(v)) return i; + if (func(v, i)) return i; } return -1; } - function forEach(this: T[], func: (v: T) => void) { - for (const v of this) func(v); + function forEach(this: T[], func: (v: T, i: number) => void) { + for (let i = 0; i < this.length; i++) func(this[i], i); } function includes(this: T[], searchElement: T, fromIndex = 0) { @@ -273,10 +274,10 @@ namespace __Array { return -1; } - function map(this: T[], func: (v: T) => V) { + function map(this: T[], func: (v: T, i: number) => V) { let result: V[] = []; result.length = this.length; - for (let i = 0; i in this; i++) result[i] = func(this[i]); + for (let i = 0; i in this; i++) result[i] = func(this[i], i); return result; } @@ -360,8 +361,8 @@ namespace __Array { return newArray; } - function some(this: T[], func: (v: T) => boolean) { - for (const v of this) if (func(v)) return true; + function some(this: T[], func: (v: T, i: number) => boolean) { + for (let i = 0; i < this.length; i++) if (func(this[i], i)) return true; return false; } @@ -553,7 +554,7 @@ class Array { return this.join(); } - public every(func: (v: T) => boolean) { + public every(func: (v: T, i: number) => boolean) { return this.data.every(func); } @@ -561,32 +562,32 @@ class Array { return this.data.fill(value, start, end); } - public filter(func: (v: T) => boolean) { + public filter(func: (v: T, i: number) => boolean) { return this.data.filter(func); } @borrows("this", "func") - public filter2(func: (v: T) => boolean) { + public filter2(func: (v: T, i: number) => boolean) { return this.data.filter2(func); } - public find(func: (v: T) => boolean) { + public find(func: (v: T, i: number) => boolean) { return this.data.find(func); } - public findIndex(func: (v: T) => boolean) { + public findIndex(func: (v: T, i: number) => boolean) { return this.data.findIndex(func); } - public findLast(func: (v: T) => boolean) { + public findLast(func: (v: T, i: number) => boolean) { return this.data.findLast(func); } - public findLastIndex(func: (v: T) => boolean) { + public findLastIndex(func: (v: T, i: number) => boolean) { return this.data.findLastIndex(func); } - public forEach(func: (v: T) => void) { + public forEach(func: (v: T, i: number) => void) { this.data.forEach(func); } @@ -611,7 +612,7 @@ class Array { return this.data.lastIndexOf(searchElement, fromIndex); } - public map(func: (v: T) => V) { + public map(func: (v: T, i: number) => V) { return this.data.map(func); } @@ -631,7 +632,7 @@ class Array { return this.data.slice(start, end); } - public some(func: (v: T) => boolean) { + public some(func: (v: T, i: number) => boolean) { return this.data.some(func); } diff --git a/tests/array_callback_index.ts b/tests/array_callback_index.ts new file mode 100644 index 0000000..61aeae9 --- /dev/null +++ b/tests/array_callback_index.ts @@ -0,0 +1,18 @@ +// An array's callbacks take the element and its index, as JavaScript gives them (`xs.map((x, i) => x + i)`); +// one that takes the element only still works. +function main() { + const xs = [10, 20, 30]; + const k = 1; + assert(xs.map((x, i) => x + i)[2] == 32, "map with index"); + assert(xs.map((x) => x * 2)[1] == 40, "map without index"); + assert(xs.filter((x, i) => i > 0).length == 2, "filter"); + assert(xs.find((x, i) => i == 1) == 20, "find"); + assert(xs.findIndex((x, i) => x == 20 && i == 1) == 1, "findIndex"); + assert(xs.findLastIndex((x, i) => i < 2) == 1, "findLastIndex"); + let s: number = 0; + xs.forEach((x, i) => { s += x * i; }); + assert(s == 80, "forEach"); + assert(xs.some((x, i) => i == 2 && x == 30), "some"); + assert(xs.every((x, i) => x > i + k), "every"); + console.log("ALL DONE"); +}