refactor(ListWrapper): drop filter, find, reduce & any

Closes #5152
This commit is contained in:
Victor Berchet
2015-10-09 09:07:58 -07:00
parent e667ad3e6b
commit 0dcca1a28e
30 changed files with 66 additions and 98 deletions
@@ -108,25 +108,16 @@ class ListWrapper {
new List.generate(size, (_) => null, growable: true);
static bool contains(List m, k) => m.contains(k);
static List filter(List list, bool fn(item)) => list.where(fn).toList();
static int indexOf(List list, value, [int startIndex = 0]) =>
list.indexOf(value, startIndex);
static int lastIndexOf(List list, value, [int startIndex = null]) =>
list.lastIndexOf(value, startIndex == null ? list.length : startIndex);
static find(List list, bool fn(item)) =>
list.firstWhere(fn, orElse: () => null);
static bool any(List list, bool fn(item)) => list.any(fn);
static void forEachWithIndex(List list, fn(item, index)) {
for (var i = 0; i < list.length; ++i) {
fn(list[i], i);
}
}
static reduce(List list, fn(a, b), init) {
return list.fold(init, fn);
}
static first(List list) => list.isEmpty ? null : list.first;
static last(List list) => list.isEmpty ? null : list.last;
static List reversed(List list) => list.reversed.toList();
-18
View File
@@ -187,27 +187,9 @@ export class ListWrapper {
if (!array || array.length == 0) return null;
return array[array.length - 1];
}
static find<T>(list: T[], pred: Predicate<T>): T {
for (var i = 0; i < list.length; ++i) {
if (pred(list[i])) return list[i];
}
return null;
}
static indexOf<T>(array: T[], value: T, startIndex: number = 0): number {
return array.indexOf(value, startIndex);
}
static reduce<T, E>(list: T[],
fn: (accumValue: E, currentValue: T, currentIndex: number, array: T[]) => E,
init: E): E {
return list.reduce(fn, init);
}
static filter<T>(array: T[], pred: Predicate<T>): T[] { return array.filter(pred); }
static any(list: any[], pred: Function): boolean {
for (var i = 0; i < list.length; ++i) {
if (pred(list[i])) return true;
}
return false;
}
static contains<T>(list: T[], el: T): boolean { return list.indexOf(el) !== -1; }
static reversed<T>(array: T[]): T[] {
var a = ListWrapper.clone(array);