Protected
iteratorImplements this as an Iterable so it's allowed to be used with "for of" loops.
Yields non-overlapping chunks (tuples) of length
from this iterator.
iter([1,2,3,4,5,6,7,8,9]).chunk(3).toArray() // [[1,2,3], [4,5,6], [7,8,9]]
iter([1,2,3,4,5,6,7,8,9]).chunk(2, 0).toArray() // [[1,2], [3,4], [5,6], [7,8], [9, 0]]
The size of each combination.
Whether or not to allow duplicate elements in the combinations.
Returns size
length subsequences of this iterator.
An iterator or iterable of falsey or truthy values to select which values to keep in this iterator.
Filters/compresses this iterator to only values that correspond to truthy values in selectors
.
Rest
...args: UConcatenates this iterator with the given iterators, in order of: [this.iterator, ...others]
.
The number of times to cycle through the iterator (default: Infinity).
Makes this iterator cycle infinitely through it's values.
equal(iter([1,2,3]).cycle().take(5).toArray(), [1,2,3,1,2])
Distributes this iterator's values among n
amount of smaller iterators. Does not maintain order so if order is
important, use divide
instead.
Divides this iterator into n
amount of smaller iterators while maintaining order. Note, this method will fully
iterate through this iterator before returning a result. If you don't want this behavior and don't care about
order then use distribute
instead.
Drop/skip values in this iterator while the passed predicate
returns a truthy value.
Attaches the index to each value as a pair like: [0, value], [1, value]
, etc.
A function that returns a truthy value to indicate to keep that value.
Returns a new ExtendedIterator that filters each element in this iterator.
Maps and filters the input iterator in the same iteratee
function.
Finds the first value that passes a truthy value to predicate
, then returns it. Only consumes the iterator's
values up to the found value, then stops. So if it's not found, then the iterator is exhausted.
Maps this iterator to a new value R
and flattens any resulting iterables or iterators by a depth of 1.
Behaves the same as Array.prototype.flatMap
.
The number of levels to flatten (default: Infinity, i.e. deeply).
Flatten this iterator by a known depth or deeply.
Groups consecutive keys in the input iterator by some key identifier function or property. Functionally equivalent to the itertools.groupby function in Python. Generally the input iterator needs to be sorted by the same key. Each iteration will return the next group of values with the same key, until the key changes. So unless the input iterator is sorted beforehand, you may have broken up groups with the same keys.
groupby('AAAABBBCCDAABBB') // =>
// ['A', ['A', 'A', 'A', 'A']],
// ['B', ['B', 'B', 'B']],
// ['C', ['C', 'C']],
// ['D', ['D']],
// ['A', ['A', 'A']],
// ['B', ['B', 'B', 'B']]
Returns true if value
strictly equals some value in this iterator.
Returns a new ExtendedIterator that maps each element in this iterator to a new value.
Return a new iterator of pairs (tuples) of the values in this one. The number of pairs will always be one fewer than this iterator. Will be empty if this iterator has fewer than two values.
iter([1,2,3]).pairwise().toArray() // [[1,2], [2,3]]
iter([1]).pairwise().toArray() // []
Optional
size: SizeThe size of each permutation, must be greater than 0 and less than or equal to the length of this iterator.
Returns all successive size
length permutations of this iterator. The permutations are emitted in lexicographic
ordering according to this iterator. So if this iterator is sorted, the permutations will be in sorted order.
Elements in the permutations are treated as unique based on their position in the iterator, not on their value. So
if the input iterator is unique, then there will be no repeat values.
https://docs.python.org/3/library/itertools.html#itertools.permutations for more info.
Maps key
from T
in each value of this iterator.
Rest
...args: UPrepends this iterator with the given iterators, in order of: [...args, this.iterator]
.
Optional
repeat: numberOptional number of times to repeat (default: 1).
Returns the cartesian product of this iterator with other iterators
after it.
https://docs.python.org/3/library/itertools.html#itertools.product for more info.
Optional
repeat: numberThe number of times to resume the iterator (default: Infinity).
Resumes this iterator a certain number of times after it's next value returns { done: true }
.
const it = iter([1,2,3]).resume(1);
equal(it.toArray(), [1,2,3]);
equal(it.toArray(), [1,2,3]);
equal(it.toArray(), []);
Reverses this iterator's order. Note that in order to reverse, it will attempt to iterate fully once, which could cause significant memory usage. So because of this, only use on finite iterators.
Collects all values from this iterator, then shuffles the order of it's values.
The index to start at (inclusive).
The index to end at (exclusive).
A new ExtendedIterator that only includes the elements between start
and end
.
Works like Array.prototype.slice
, returns a new slice of this iterator.
This does not support negative start
and end
indices, as it's not possible to know the length of the
iterator while iterating.
Collects all values from this iterator, then sorts them.
Take all elements from this iterator while the given predicate
returns a truthy value.
Tap into this iterator by supplying func
which is passed each value of this iterator. The return value of
func is unused and this method is purely designed for a designated place to perform side effects.
iter([1,2,3])
.tap(console.log) // logs 1, 2, 3 to the console
.map(n => n * n)
.tap(console.log) // logs 1, 4, 9 to the console
.toArray() // returns [1, 4, 9]
The number of independent iterators to create.
Returns n
independent iterators, each of which is a copy of this iterator at the time of calling tee
. Once
tee
has made a split, do not modify or call upon the original iterator, as the new iterators will not be
updated/informed.
This caches the original iterator's values as the new iterators are iterated through. So
depending on the size of the original iterator, there could be significant memory overhead in using tee
.
tee
's intended use is to iterate over the returned iterators in parallel, or at least somewhat in parallel. In
general, if one returned iterator consumes most or all of it's values, then it is faster to just
use toArray
and then iterate over that.
Returns a new iterator of triplets (tuples) of the values in this one. The number of triplets will always be two fewer than the number of values in this iterator. Will be empty if this iterator has fewer than three values.
Filters this iterator to only unique values.
The inverse of zip
and zipLongest
. This method disaggregates the elements of this iterator. The nth iterator
in the returned tuple contains the nth element of each value in this iterator. The length of the returned tuple is
determined by the length of the first value in this iterator.
Yields sliding windows (tuples) of length
from this iterator. Each window is separated by offset
number of
elements.
iter([1,2,3,4,5]).windows(2, 1).toArray() // [[1,2], [2,3], [3,4], [4,5]]
iter([1,2,3,4,5]).windows(2, 3).toArray() // [[1,2], [4,5]]
iter([1,2,3,4,5]).windows(3, 3, 0).toArray() // [[1,2,3], [4,5,0]]
Aggregates this iterator and any number of others into one. Stops when one of the iterables is empty.
Rest
...args: IteratorOrIterable<any>[]Aggregates this iterator and any number of others into one. Stops when all of the iterables is empty.
Rest
...args: IteratorOrIterable<any>[]
Extends and implements the IterableIterator interface. Methods marked with the
@lazy
prefix are chainable methods that modify the internal iterator, but don't start iterating. Methods without the@lazy
prefix do start iterating some amount, depending on the method.