Skip to content

Commit fac0fc5

Browse files
MaxGraeydcodeIO
authored andcommitted
Implement Array#map, Array#forEach, Array#filter, Array#reduceRight (AssemblyScript#81)
1 parent 05117f9 commit fac0fc5

File tree

6 files changed

+9774
-6526
lines changed

6 files changed

+9774
-6526
lines changed

std/assembly.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,14 +337,18 @@ declare class Array<T> {
337337
lastIndexOf(searchElement: T, fromIndex?: i32): i32;
338338
push(element: T): void;
339339
pop(): T;
340+
forEach(callbackfn: (value: T, index: i32, array: Array<T>) => void): void;
341+
map<U>(callbackfn: (value: T, index: i32, array: Array<T>) => U): Array<U>;
342+
filter(callbackfn: (value: T, index: i32, array: Array<T>) => bool): Array<T>;
340343
reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: i32, array: Array<T>) => U, initialValue: U): U;
344+
reduceRight<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: i32, array: Array<T>) => U, initialValue: U): U;
341345
shift(): T;
342346
some(callbackfn: (element: T, index: i32, array?: Array<T>) => bool): bool;
343347
unshift(element: T): i32;
344348
slice(from: i32, to?: i32): T[];
345349
splice(start: i32, deleteCount?: i32): void;
346350
reverse(): T[];
347-
sort(comparator?: (a: T, b: T) => i32): T[];
351+
sort(comparator?: (a: T, b: T) => i32): this;
348352
}
349353

350354
/** Class representing a C-like array of values of type `T` with limited capabilities. */

std/assembly/array.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,35 @@ export class Array<T> {
142142
return element;
143143
}
144144

145+
forEach(callbackfn: (value: T, index: i32, array: Array<T>) => void): void {
146+
var buffer = this.buffer_;
147+
for (let index = 0, toIndex = this.length_; index < toIndex && index < this.length_; ++index) {
148+
callbackfn(loadUnsafe<T>(buffer, index), index, this);
149+
}
150+
}
151+
152+
map<U>(callbackfn: (value: T, index: i32, array: Array<T>) => U): Array<U> {
153+
var buffer = this.buffer_;
154+
var length = this.length_;
155+
var result = new Array<U>(length);
156+
var resultBuffer = result.buffer_;
157+
for (let index = 0; index < length && index < this.length_; ++index) {
158+
storeUnsafe<U>(resultBuffer, index, callbackfn(loadUnsafe<T>(buffer, index), index, this));
159+
}
160+
return result;
161+
}
162+
163+
filter(callbackfn: (value: T, index: i32, array: Array<T>) => bool): Array<T> {
164+
var buffer = this.buffer_;
165+
var length = this.length_;
166+
var result = new Array<T>();
167+
for (let index = 0; index < length && index < this.length_; ++index) {
168+
let value = loadUnsafe<T>(buffer, index);
169+
if (callbackfn(value, index, this)) result.push(value);
170+
}
171+
return result;
172+
}
173+
145174
reduce<U>(
146175
callbackfn: (previousValue: U, currentValue: T, currentIndex: i32, array: Array<T>) => U,
147176
initialValue: U
@@ -154,6 +183,18 @@ export class Array<T> {
154183
return accum;
155184
}
156185

186+
reduceRight<U>(
187+
callbackfn: (previousValue: U, currentValue: T, currentIndex: i32, array: Array<T>) => U,
188+
initialValue: U
189+
): U {
190+
var accum = initialValue;
191+
var buffer = this.buffer_;
192+
for (let index: i32 = this.length_ - 1; index >= 0; --index) {
193+
accum = callbackfn(accum, loadUnsafe<T>(buffer, index), index, this);
194+
}
195+
return accum;
196+
}
197+
157198
shift(): T {
158199
var length = this.length_;
159200
if (length < 1) throw new RangeError("Array is empty");
@@ -245,7 +286,7 @@ export class Array<T> {
245286
return this;
246287
}
247288

248-
sort(comparator: (a: T, b: T) => i32 = defaultComparator<T>()): Array<T> {
289+
sort(comparator: (a: T, b: T) => i32 = defaultComparator<T>()): this {
249290
var length = this.length_;
250291
if (length <= 1) return this;
251292
var buffer = this.buffer_;

std/portable.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,18 @@ declare class Array<T> {
237237
lastIndexOf(searchElement: T, fromIndex?: i32): i32;
238238
push(element: T): void;
239239
pop(): T;
240+
forEach(callbackfn: (value: T, index: i32, array: Array<T>) => void): void;
241+
map<U>(callbackfn: (value: T, index: i32, array: Array<T>) => U): Array<U>;
242+
filter(callbackfn: (value: T, index: i32, array: Array<T>) => bool): Array<T>;
240243
reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: i32, array: Array<T>) => U, initialValue: U): U;
244+
reduceRight<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: i32, array: Array<T>) => U, initialValue: U): U;
241245
shift(): T;
242246
some(callbackfn: (element: T, index: i32, array?: Array<T>) => bool): bool;
243247
unshift(element: T): i32;
244248
slice(from: i32, to?: i32): T[];
245249
splice(start: i32, deleteCount?: i32): void;
246250
reverse(): T[];
247-
sort(comparator?: (a: T, b: T) => i32): T[];
251+
sort(comparator?: (a: T, b: T) => i32): this;
248252

249253
join(delim: string): string;
250254
}

0 commit comments

Comments
 (0)