Iterator helper 函数
大约 2 分钟
Iterator
helper 函数
注
这是一个 ECMAScript 提案,请不要把它与 Core-JS 提供的工具函数混淆。
模块
esnext.iterator.constructor
esnext.iterator.drop
esnext.iterator.every
esnext.iterator.filter
esnext.iterator.find
esnext.iterator.flat-map
esnext.iterator.for-each
esnext.iterator.from
esnext.iterator.indexed
esnext.iterator.map
esnext.iterator.reduce
esnext.iterator.some
esnext.iterator.take
esnext.iterator.to-array
类型
interface Iterator<T> {
drop(limit: number): Iterator<T>;
every(callbackfn: (value: T) => boolean): boolean;
filter(callbackfn: (value: T) => boolean): Iterator<T>;
find(callbackfn: (value: T) => boolean): T;
flatMap<U>(
callbackfn: (value: T, couner: number) => Iterable<U> | Iterator<U>
): Iterator<U>;
forEach(callbackfn: (value: T) => void): void;
indexed(): Iterator<[number, T]>;
map<U>(callbackfn: (value: T) => U): Iterator<U>;
reduce<U>(callbackfn: (memo: U, value: T) => U, initialValue: U): U;
some(callbackfn: (value: T) => boolean): boolean;
take(limit: number): Iterator<T>;
toArray(): Array<T>;
[Symbol.toStringTag]: "Iterator";
}
interface IteratorConstructor {
from<T>(iterable: Iterable<T> | Iterator<T>): Iterator<T>;
}
入口点
core-js/proposals/iterator-helpers
core-js(-pure)/full/async-iterator
core-js(-pure)/full/async-iterator/drop
core-js(-pure)/full/async-iterator/every
core-js(-pure)/full/async-iterator/filter
core-js(-pure)/full/async-iterator/find
core-js(-pure)/full/async-iterator/flat-map
core-js(-pure)/full/async-iterator/for-each
core-js(-pure)/full/async-iterator/from
core-js(-pure)/full/async-iterator/indexed
core-js(-pure)/full/async-iterator/map
core-js(-pure)/full/async-iterator/reduce
core-js(-pure)/full/async-iterator/some
core-js(-pure)/full/async-iterator/take
core-js(-pure)/full/async-iterator/to-array
core-js(-pure)/full/iterator
core-js(-pure)/full/iterator/drop
core-js(-pure)/full/iterator/every
core-js(-pure)/full/iterator/filter
core-js(-pure)/full/iterator/find
core-js(-pure)/full/iterator/flat-map
core-js(-pure)/full/iterator/for-each
core-js(-pure)/full/iterator/from
core-js(-pure)/full/iterator/indexed
core-js(-pure)/full/iterator/map
core-js(-pure)/full/iterator/reduce
core-js(-pure)/full/iterator/some
core-js(-pure)/full/iterator/take
core-js(-pure)/full/iterator/to-array
示例
示例:
[1, 2, 3, 4, 5, 6, 7]
.values()
.drop(1)
.take(5)
.filter((it) => it % 2)
.map((it) => it ** 2)
.toArray(); // => [9, 25]
Iterator.from({
next: () => ({ done: Math.random() > 0.9, value: (Math.random() * 10) | 0 }),
}).toArray(); // => [7, 6, 3, 0, 2, 8]
await AsyncIterator.from([1, 2, 3, 4, 5, 6, 7])
.drop(1)
.take(5)
.filter((it) => it % 2)
.map((it) => it ** 2)
.toArray(); // => [9, 25]
await [1, 2, 3]
.values()
.toAsync()
.map(async (it) => it ** 2)
.toArray(); // => [1, 4, 9]
注意事项
- 为了避免污染原型,在
pure
版本中,新的%IteratorPrototype%
方法没有被加入到真正的%IteratorPrototype%
中,它们只在 wrapper 中可用——使用AIterator.from([]).map(fn)
代替[].values().map(fn)
。