Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 32 additions & 31 deletions src/generics/lib.generics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,10 @@ namespace __Array {
return this.join();
}

function every<T>(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<T>(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;
}

Expand Down Expand Up @@ -150,47 +152,46 @@ namespace __Array {
return newArray;
}

function filter<T>(this: T[], func: (v: T) => boolean) {
function filter<T>(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<T>(this: T[], func: (v: T) => boolean) {
for (const v of this) if (func(v)) yield v;
function* filter2<T>(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<T>(this: T[], func: (v: T) => boolean): T | undefined {
for (const v of this) if (func(v)) return v;
function find<T>(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<T>(this: T[], func: (v: T) => boolean) {
let i = -1;
for (const v of this) if (i++, func(v)) return i;
function findIndex<T>(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<T>(this: T[], func: (v: T) => boolean) {
function findLast<T>(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<T>(this: T[], func: (v: T) => boolean) {
function findLastIndex<T>(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<T>(this: T[], func: (v: T) => void) {
for (const v of this) func(v);
function forEach<T>(this: T[], func: (v: T, i: number) => void) {
for (let i = 0; i < this.length; i++) func(this[i], i);
}

function includes<T>(this: T[], searchElement: T, fromIndex = 0) {
Expand Down Expand Up @@ -273,10 +274,10 @@ namespace __Array {
return -1;
}

function map<T, V>(this: T[], func: (v: T) => V) {
function map<T, V>(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;
}

Expand Down Expand Up @@ -360,8 +361,8 @@ namespace __Array {
return newArray;
}

function some<T>(this: T[], func: (v: T) => boolean) {
for (const v of this) if (func(v)) return true;
function some<T>(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;
}

Expand Down Expand Up @@ -553,40 +554,40 @@ class Array<T> {
return this.join();
}

public every(func: (v: T) => boolean) {
public every(func: (v: T, i: number) => boolean) {
return this.data.every(func);
}

public fill(value: T, start = 0, end = this.length) {
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);
}

Expand All @@ -611,7 +612,7 @@ class Array<T> {
return this.data.lastIndexOf(searchElement, fromIndex);
}

public map<V>(func: (v: T) => V) {
public map<V>(func: (v: T, i: number) => V) {
return this.data.map<V>(func);
}

Expand All @@ -631,7 +632,7 @@ class Array<T> {
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);
}

Expand Down
18 changes: 18 additions & 0 deletions tests/array_callback_index.ts
Original file line number Diff line number Diff line change
@@ -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");
}