class Extensions
package itertools
Extension functions to provide a fluent iterator API. Use with:
using itertools.Extensions;Static methods
staticall<T>(it:Iterable<T>, pred:T ‑> Bool):Bool
Checks, if a given predicate is true for all elements of an iterable.
Parameters:
it | The iterable that contains the elements to check the predicate for. |
|---|
Returns:
true, if all elements of it returned true for pred,
false otherwise.
staticany<T>(it:Iterable<T>, pred:T ‑> Bool):Bool
Checks, if a given predicate is true for any element of an iterable.
Parameters:
it | The iterable that contains the elements to check the predicate for. |
|---|
Returns:
true, if any element of it returned true for pred,
false otherwise.
staticasIterable<T>(it:Iterable<T>):Iterable<T>
Utility to cast an iterable type to an Iterable<T>.
Useful, if using the extension functions like map on an Array, so
the extension is invoked instead of the built-in.
staticasOnceIterable<T>(it:Iterator<T>):Iterable<T>
Utility to cast an iterator to an Iterable<T>.
Useful for things like using Map<T>.keys() to use in the API.
staticchain<T>(it1:Iterable<T>, it2:Iterable<T>):Iterable<T>
Chains together 2 iterables one after the other.
Parameters:
it1 | The first iterable in the chain. |
|---|---|
it2 | The second iterable to chain. |
Returns:
An iterable that first contains the elements of it1, then the
elements of it2.
staticcount<T>(it:Iterable<T>):Int
Counts the number of elements in an iterable.
Parameters:
it | The iterable to count the number of elements of. |
|---|
Returns:
The number of elements in it.
staticenumerate<T>(it:Iterable<T>):Iterable<Indexed<T>>
Adds the 0-based index for each element of an iterable.
Parameters:
it | The iterable to add the index of each element for. |
|---|
Returns:
The iterable that contains each element of it in an
Indexed.
staticfilter<T>(it:Iterable<T>, pred:T ‑> Bool):Iterable<T>
Filters the elements of an iterable using a predicate.
Parameters:
it | The iterable to filter the elements of. |
|---|---|
pred | The filtering predicate. |
Returns:
The iterable that uses pred to filter the elements of it.
staticfilterMap<T, U>(it:Iterable<T>, f:T ‑> Option<U>):Iterable<U>
Filters and maps an iterable by transforming to an Optional<U>, and
only keeping Some(U).
Parameters:
it | The iterable to filter and map. |
|---|---|
f | The function to use for transforming. |
Returns:
The iterable that maps and filters it with f.
staticfind<T>(it:Iterable<T>, pred:T ‑> Bool):Option<T>
Attempts to find the first element in an iterable that matches the given predicate.
Parameters:
it | The iterable to search. |
|---|---|
pred | The predicate to search elements with. |
Returns:
The first element of it, where pred returns true, None
otherwise.
staticfirst<T>(it:Iterable<T>):Option<T>
Retrieves the first element of an iterable, if any.
Parameters:
it | The iterable to retrieve the first element of. |
|---|
Returns:
The first element of it, or None, if it was empty.
staticflatMap<T, U>(it:Iterable<T>, f:T ‑> Iterable<U>):Iterable<U>
Maps the elements of an iterator to multiple other elements that get flattened.
Parameters:
it | The iterable elements to map. |
|---|---|
f | The function to transform the elements. |
Returns:
The elements of it, transformed by f, flattened into a
single iterable.
staticflatten<T>(it:Iterable<Iterable<T>>):Iterable<T>
Flattens a nested iterable structure.
Parameters:
it | The nested iterable to flatten. |
|---|
Returns:
The flattened elements of it, containing each element of each
iterable in it.
staticfoldl<T, U>(it:Iterable<T>, seed:U, acc:(U, T) ‑> U):U
Accumulates the elements of an iterable from left to right.
Parameters:
it | The iterable that is fed into the accumulator. |
|---|---|
seed | The initial partial result. |
acc | The accumulator function. |
Returns:
The result of the accumulation.
staticgroupBy<K, T>(it:Iterable<T>, keySel:T ‑> K):Iterable<Grouping<K, T>>
Groups the elements of an iterable.
Parameters:
it | The iterable to group the elements of. |
|---|---|
keySel | The selector function that selects the grouping key for each element. |
Returns:
An iterable that contains the groups constructed from the
elements of it with the group key selected with keySel in
Groupings.
staticjoin<T>(it:Iterable<T>, sep:String):String
Joins iterable elements with a separator into a string.
The elements are added using StringBuf.add.
Parameters:
it | The iterable to join the elements of. |
|---|---|
sep | The separator string. |
Returns:
The items of it joined into a String, separated by sep.
staticlast<T>(it:Iterable<T>):Option<T>
Retrieves the last element of an iterable, if any.
Parameters:
it | The iterable to retrieve the last element of. |
|---|
Returns:
The last element of it, or None, if it was empty.
staticmap<T, U>(it:Iterable<T>, f:T ‑> U):Iterable<U>
Maps the elements of an iterable using a transformer function.
Parameters:
it | The iterable to transform the elements of. |
|---|---|
f | The transformer function. |
Returns:
The iterable that uses f to transform each element of it.
staticnth<T>(it:Iterable<T>, n:Int):Option<T>
Retrieves the nth element of an iterable, if any.
Parameters:
it | The iterable to retrieve the nth element of. |
|---|---|
n | The 0-based index of the element to retrieve. |
Returns:
The nth element of it, or None, if it was empty.
staticreverse<T>(it:Iterable<T>):Iterable<T>
Reverses an iterable.
Parameters:
it | The iterable to reverse. |
|---|
Returns:
An iterable that contains the elements of it, but in reverse
order.
staticscanl<T, U>(it:Iterable<T>, seed:U, acc:(U, T) ‑> U):Iterable<U>
Repeatedly applies an accumulator function for the partial result and the next element of an iterable.
Parameters:
it | The iterable that is fed into the accumulator. |
|---|---|
seed | The initial partial result. |
acc | The accumulator function. |
Returns:
An iterable that contains each element of the accumulation.
staticskip<T>(it:Iterable<T>, n:Int):Iterable<T>
Skips the first n elements of an iterable.
Parameters:
it | The iterable to skip the first |
|---|---|
n | The amount of elements to skip. |
Returns:
An iterable that misses the first n elements of it.
staticskipWhile<T>(it:Iterable<T>, pred:T ‑> Bool):Iterable<T>
Skips the first elements of an iterable, while a predicate is true for the elements.
@param it The iterable to skip the first elements of `it`.
@param pred The predicate to use when skipping elements.
@returns An iterable that misses the first elements of `it`, while
pred returns true.
statictake<T>(it:Iterable<T>, n:Int):Iterable<T>
Only keeps the first n elements of an iterable.
Parameters:
it | The iterable to keep the first |
|---|---|
n | The amount of elements to keep. |
Returns:
An iterable that only keeps the first n elements of it.
statictakeWhile<T>(it:Iterable<T>, pred:T ‑> Bool):Iterable<T>
Only keeps the first elements of an iterable, while a predicate is true for the elements.
Parameters:
it | The iterable to keep the first elements of |
|---|---|
pred | The predicate to use when keeping elements. |
Returns:
An iterable that keeps the first elements of it, while pred
returns true.
statictoArray<T>(it:Iterable<T>):Array<T>
Collects the elements of an iterable to an Array.
Parameters:
it | The iterable to collect the elements of. |
|---|
Returns:
The items of it collected into an Array.
statictoMap<T, K>(it:Iterable<T>, keySel:T ‑> K):Map<K, T>
Collects the elements of an iterable to a Map with a given key
selector.
Parameters:
it | The iterable to collect the elements of. |
|---|---|
keySel | The function to select the key from the elements of |
Returns:
The items of it collected into a Map.
statictoMapProj<T, K, V>(it:Iterable<T>, keySel:T ‑> K, valSel:T ‑> V):Map<K, V>
Collects the elements of an iterable to a Map with a given key and
value selector.
Parameters:
it | The iterable to collect the elements of. |
|---|---|
keySel | The function to select the key from the elements of |
valSel | The function to select the value from the elements of
|
Returns:
The items of it collected into a Map.