Wrapper class containing methods to modify iterators.
class ref Iter[A: A] is Iterator[A] ref
new ref create( iter: Iterator[A] ref) : Iter[A] ref^
Take an iterator of iterators and return an Iter containing the items of the first one, then the second one, and so on.
let xs = [as I64: 1; 2].values() let ys = [as I64: 3; 4].values() Iter[I64].chain([xs; ys].values())
1 2 3 4
new ref chain( outer_iterator: Iterator[Iterator[A] ref] ref) : Iter[A] ref^
Create an iterator that returns the given value forever.
Iter[U32].repeat_value(7)
7 7 7 7 7 7 7 7 7 ...
new ref repeat_value( value: A) : Iter[A] ref^
fun ref has_next() : Bool val
fun ref next() : A ?
Allows stateful transformaion of each element from the iterator, similar to map
.
fun ref map_stateful[B: B]( f: {ref(A!): B^ ?}[A, B] ref) : Iter[B] ref^
Allows filtering of elements based on a stateful adapter, similar to filter
.
fun ref filter_stateful( f: {ref(A!): Bool ?}[A] ref) : Iter[A!] ref^
Allows stateful modification to the stream of elements from an iterator, similar to filter_map
.
fun ref filter_map_stateful[B: B]( f: {ref(A!): (B^ | None) ?}[A, B] ref) : Iter[B] ref^
Return false if at least one value of the iterator fails to match the predicate f
. This method short-circuits at the first value where the predicate returns false, otherwise true is returned.
Iter[I64]([2; 4; 6].values()) .all({(x) => (x % 2) == 0 })
true
Iter[I64]([2; 3; 4].values()) .all({(x) => (x % 2) == 0 })
false
fun ref all( f: {(A!): Bool ?}[A] box) : Bool val
Return true if at least one value of the iterator matches the predicate f
. This method short-circuits at the first value where the predicate returns true, otherwise false is returned.
Iter[I64]([2; 4; 6].values()) .any({(I64) => (x % 2) == 1 })
false
Iter[I64]([2; 3; 4].values()) .any({(I64) => (x % 2) == 1 })
true
fun ref any( f: {(A!): Bool ?}[A] box) : Bool val
Push each value from the iterator into the collection coll
.
Iter[I64]([1; 2; 3].values()) .collect(Array[I64](3))
[1, 2, 3]
fun ref collect[optional B: Seq[A!] ref]( coll: B) : B^
Return the number of values in the iterator.
Iter[I64]([1; 2; 3].values()) .count()
3
fun ref count() : USize val
Repeatedly cycle through the values from the iterator.
WARNING: The values returned by the original iterator are cached, so the input iterator should be finite.
Iter[I64]([1; 2; 3].values()) .cycle()
1 2 3 1 2 3 1 2 3 ...
fun ref cycle() : Iter[A!] ref^
An iterator which yields the current iteration count as well as the next value from the iterator.
Iter[I64]([1; 2; 3].values()) .enum()
(0, 1) (1, 2) (2, 3)
fun ref enum[optional B: (Real[B] val & (I8 val | I16 val | I32 val | I64 val | I128 val | ILong val | ISize val | U8 val | U16 val | U32 val | U64 val | U128 val | ULong val | USize val | F32 val | F64 val))]() : Iter[(B , A)] ref^
Return an iterator that only returns items that match the predicate f
.
Iter[I64]([1; 2; 3; 4; 5; 6].values()) .filter({(x) => (x % 2) == 0 })
2 4 6
fun ref filter( f: {(A!): Bool ?}[A] box) : Iter[A!] ref^
Return the nth value in the iterator that satisfies the predicate f
.
Iter[I64]([1; 2; 3].values()) .find({(x) => (x % 2) == 0 })
2
Iter[I64]([1; 2; 3; 4].values()) .find({(x) => (x % 2) == 0 }, 2)
4
fun ref find( f: {(A!): Bool ?}[A] box, n: USize val = 1) : A! ?
Return an iterator which applies f
to each element. If None
is returned, then the iterator will try again by applying f
to the next element. Otherwise, the value of type B
is returned.
Iter[I64]([as I64: 1; -2; 4; 7; -5]) .filter_map[USize]( {(i: I64): (USize | None) => if i >= 0 then i.usize() end })
1 4 7
```pony fun ref filter_map[B: B]( f: {(A!): (B^ | None) ?}[A, B] box) : Iter[B] ref^
Return an iterator over the values of the iterators produced from the application of the given function.
Iter[String](["alpha"; "beta"; "gamma"]) .flat_map[U8]({(s: String): Iterator[U8] => s.values() })
a l p h a b e t a g a m m a
fun ref flat_map[B: B]( f: {(A!): Iterator[B] ?}[A, B] box) : Iter[B] ref^
Apply a function to every element, producing an accumulated value.
Iter[I64]([1; 2; 3].values()) .fold[I64](0, {(sum, x) => sum + x })
6
fun ref fold[B: B]( acc: B, f: {(B, A!): B^}[A, B] box) : B^
A partial version of fold
.
fun ref fold_partial[B: B]( acc: B, f: {(B, A!): B^ ?}[A, B] box) : B^ ?
Return the last value of the iterator.
Iter[I64]([1; 2; 3].values()) .last()
3
fun ref last() : A ?
Return an iterator where each item's value is the application of the given function to the value in the original iterator.
Iter[I64]([1; 2; 3].values()) .map[I64]({(x) => x * x })
1 4 9
fun ref map[B: B]( f: {(A!): B^ ?}[A, B] box) : Iter[B] ref^
Return the nth value of the iterator.
Iter[I64]([1; 2; 3].values()) .nth(2)
2
fun ref nth( n: USize val) : A ?
Iterate through the values of the iterator without a for loop. The function on_error
will be called if the iterator's has_next
method returns true but its next
method throws an error.
Iter[I64]([1; 2; 3].values()) .map[None]({(x) => env.out.print(x.string()) }) .run()
1 2 3
fun ref run( on_error: {ref()}[A] ref = lambda) : None val
Skip the first n values of the iterator.
Iter[I64]([1; 2; 3; 4; 5; 6].values()) .skip(3)
4 5 6
Iter[I64]([1; 2; 3].values()) .skip(3) .has_next()
false
fun ref skip( n: USize val) : Iter[A] ref^
Skip values of the iterator while the predicate f
returns true.
Iter[I64]([1; 2; 3; 4; 5; 6].values()) .skip_while({(x) => x < 4 })
4 5 6
fun ref skip_while( f: {(A!): Bool ?}[A] box) : Iter[A!] ref^
Return an iterator for the first n elements.
Iter[I64]([1; 2; 3; 4; 5; 6].values()) .take(3)
1 2 3
fun ref take( n: USize val) : Iter[A] ref^
Return an iterator that returns values while the predicate f
returns true. This iterator short-circuits the first time that f
returns false or raises an error.
Iter[I64]([1; 2; 3; 4; 5; 6].values()) .take_while({(x) => x < 4 })
1 2 3
fun ref take_while( f: {(A!): Bool ?}[A] box) : Iter[A!] ref^
Zip two iterators together so that each call to next() results in a tuple with the next value of the first iterator and the next value of the second iterator. The number of items returned is the minimum of the number of items returned by the two iterators.
Iter[I64]([1; 2].values()) .zip[I64]([3; 4].values())
(1, 3) (2, 4)
fun ref zip[B: B]( i2: Iterator[B] ref) : Iter[(A , B)] ref^
Zip three iterators together so that each call to next() results in a tuple with the next value of the first iterator, the next value of the second iterator, and the value of the third iterator. The number of items returned is the minimum of the number of items returned by the three iterators.
fun ref zip2[B: B, C: C]( i2: Iterator[B] ref, i3: Iterator[C] ref) : Iter[(A , B , C)] ref^
Zip four iterators together so that each call to next() results in a tuple with the next value of each of the iterators. The number of items returned is the minimum of the number of items returned by the iterators.
fun ref zip3[B: B, C: C, D: D]( i2: Iterator[B] ref, i3: Iterator[C] ref, i4: Iterator[D] ref) : Iter[(A , B , C , D)] ref^
Zip five iterators together so that each call to next() results in a tuple with the next value of each of the iterators. The number of items returned is the minimum of the number of items returned by the iterators.
fun ref zip4[B: B, C: C, D: D, E: E]( i2: Iterator[B] ref, i3: Iterator[C] ref, i4: Iterator[D] ref, i5: Iterator[E] ref) : Iter[(A , B , C , D , E)] ref^
© 2016-2018, The Pony Developers
© 2014-2015, Causality Ltd.
Licensed under the BSD 2-Clause License.
https://stdlib.ponylang.io/itertools-Iter