Skip to content

Commit 710fcef

Browse files
PinkaminaDianePiedcodeIO
authored andcommitted
Add array methods: findIndex, reduce, some, every (AssemblyScript#49)
1 parent 38a0259 commit 710fcef

File tree

6 files changed

+3880
-935
lines changed

6 files changed

+3880
-935
lines changed

std/assembly.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,16 @@ declare class Array<T> {
273273
length: i32;
274274
/** Constructs a new array. */
275275
constructor(capacity?: i32);
276+
every(callbackfn: (element: T, index: i32, array?: Array<T>) => bool): bool;
277+
findIndex(predicate: (element: T, index: i32, array?: Array<T>) => bool): i32;
276278
includes(searchElement: T, fromIndex?: i32): bool;
277279
indexOf(searchElement: T, fromIndex?: i32): i32;
278280
lastIndexOf(searchElement: T, fromIndex?: i32): i32;
279281
push(element: T): void;
280282
pop(): T;
283+
reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: i32, array: Array<T>) => U, initialValue: U): U;
281284
shift(): T;
285+
some(callbackfn: (element: T, index: i32, array?: Array<T>) => bool): bool;
282286
unshift(element: T): i32;
283287
slice(from: i32, to?: i32): T[];
284288
splice(start: i32, deleteCount?: i32): void;

std/assembly/array.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,30 @@ export class Array<T> {
2525
this.__capacity = this.__length = capacity;
2626
}
2727

28+
every(callbackfn: (element: T, index: i32, array: Array<T>) => bool): bool {
29+
var toIndex: i32 = this.__length;
30+
var i: i32 = 0;
31+
while (i < toIndex && i < this.__length) {
32+
if (!callbackfn(load<T>(this.__memory + <usize>i * sizeof<T>()), i, this)) {
33+
return false;
34+
}
35+
i += 1;
36+
}
37+
return true;
38+
}
39+
40+
findIndex(predicate: (element: T, index: i32, array: Array<T>) => bool): i32 {
41+
var toIndex: i32 = this.__length;
42+
var i: i32 = 0;
43+
while (i < toIndex && i < this.__length) {
44+
if (predicate(load<T>(this.__memory + <usize>i * sizeof<T>()), i, this)) {
45+
return i;
46+
}
47+
i += 1;
48+
}
49+
return -1;
50+
}
51+
2852
get length(): i32 {
2953
return this.__length;
3054
}
@@ -129,6 +153,20 @@ export class Array<T> {
129153
return load<T>(this.__memory + <usize>--this.__length * sizeof<T>());
130154
}
131155

156+
reduce<U>(
157+
callbackfn: (previousValue: U, currentValue: T, currentIndex: i32, array: Array<T>) => U,
158+
initialValue: U
159+
): U {
160+
var accumulator: U = initialValue;
161+
var toIndex: i32 = this.__length;
162+
var i: i32 = 0;
163+
while (i < toIndex && i < this.__length) {
164+
accumulator = callbackfn(accumulator, load<T>(this.__memory + <usize>i * sizeof<T>()), i, this);
165+
i += 1;
166+
}
167+
return accumulator;
168+
}
169+
132170
shift(): T {
133171
if (this.__length < 1) {
134172
throw new RangeError("Array is empty"); // return changetype<T>(0) ?
@@ -148,6 +186,18 @@ export class Array<T> {
148186
return element;
149187
}
150188

189+
some(callbackfn: (element: T, index: i32, array: Array<T>) => bool): bool {
190+
var toIndex: i32 = this.__length;
191+
var i: i32 = 0;
192+
while (i < toIndex && i < this.__length) {
193+
if (callbackfn(load<T>(this.__memory + <usize>i * sizeof<T>()), i, this)) {
194+
return true;
195+
}
196+
i += 1;
197+
}
198+
return false;
199+
}
200+
151201
unshift(element: T): i32 {
152202
var oldCapacity = this.__capacity;
153203
if (this.__length == oldCapacity) {

std/portable.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,16 @@ declare class Array<T> {
218218
[key: number]: T;
219219
length: i32;
220220
constructor(capacity?: i32);
221+
every(callbackfn: (element: T, index: i32, array?: Array<T>) => bool): bool;
222+
findIndex(predicate: (element: T, index: i32, array?: Array<T>) => bool): i32;
221223
includes(searchElement: T, fromIndex?: i32): bool;
222224
indexOf(searchElement: T, fromIndex?: i32): i32;
223225
lastIndexOf(searchElement: T, fromIndex?: i32): i32;
224226
push(element: T): void;
225227
pop(): T;
228+
reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: i32, array: Array<T>) => U, initialValue: U): U;
226229
shift(): T;
230+
some(callbackfn: (element: T, index: i32, array?: Array<T>) => bool): bool;
227231
unshift(element: T): i32;
228232
slice(from: i32, to?: i32): T[];
229233
splice(start: i32, deleteCount?: i32): void;

0 commit comments

Comments
 (0)