Skip to content

Iter[A: A]

[Source]

Wrapper class containing methods to modify iterators.

class ref Iter[A: A] is
  Iterator[A] ref

Implements


Constructors

create

[Source]

new ref create(
  iter: Iterator[A] ref)
: Iter[A] ref^

Parameters

Returns


chain

[Source]

Take an iterator of iterators and return an Iter containing the items of the first one, then the second one, and so on.

Example

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^

Parameters

Returns


repeat_value

[Source]

Create an iterator that returns the given value forever.

Example

Iter[U32].repeat_value(7)

7 7 7 7 7 7 7 7 7 ...

new ref repeat_value(
  value: A)
: Iter[A] ref^

Parameters

  • value: A

Returns


Public Functions

has_next

[Source]

fun ref has_next()
: Bool val

Returns


next

[Source]

fun ref next()
: A ?

Returns

  • A ?

map_stateful[B: B]

[Source]

Allows stateful transformation 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^

Parameters

  • f: {ref(A!): B^ ?}[A, B] ref

Returns


filter_stateful

[Source]

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^

Parameters

  • f: {ref(A!): Bool ?}[A] ref

Returns


filter_map_stateful[B: B]

[Source]

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^

Parameters

  • f: {ref(A!): (B^ | None) ?}[A, B] ref

Returns


all

[Source]

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.

Examples

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

Parameters

  • f: {(A!): Bool ?}[A] box

Returns


any

[Source]

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.

Examples

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

Parameters

  • f: {(A!): Bool ?}[A] box

Returns


collect[optional B: Seq[A!] ref]

[Source]

Push each value from the iterator into the collection coll.

Example

Iter[I64]([1; 2; 3].values())
  .collect(Array[I64](3))

[1, 2, 3]

fun ref collect[optional B: Seq[A!] ref](
  coll: B)
: B^

Parameters

  • coll: B

Returns

  • B^

count

[Source]

Return the number of values in the iterator.

Example

Iter[I64]([1; 2; 3].values())
  .count()

3

fun ref count()
: USize val

Returns


cycle

[Source]

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.

Example

Iter[I64]([1; 2; 3].values())
  .cycle()

1 2 3 1 2 3 1 2 3 ...

fun ref cycle()
: Iter[A!] ref^

Returns


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))]

[Source]

An iterator which yields the current iteration count as well as the next value from the iterator.

Example

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^

Returns

  • Iter[(B , A)] ref^

filter

[Source]

Return an iterator that only returns items that match the predicate f.

Example

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^

Parameters

  • f: {(A!): Bool ?}[A] box

Returns


find

[Source]

Return the nth value in the iterator that satisfies the predicate f.

Examples

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! ?

Parameters

  • f: {(A!): Bool ?}[A] box
  • n: USize val = 1

Returns

  • A! ?

filter_map[B: B]

[Source]

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.

Example

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^

Parameters

  • f: {(A!): (B^ | None) ?}[A, B] box

Returns


flat_map[B: B]

[Source]

Return an iterator over the values of the iterators produced from the application of the given function.

Example

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^

Parameters

  • f: {(A!): Iterator[B] ?}[A, B] box

Returns


fold[B: B]

[Source]

Apply a function to every element, producing an accumulated value.

Example

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^

Parameters

  • acc: B
  • f: {(B, A!): B^}[A, B] box

Returns

  • B^

fold_partial[B: B]

[Source]

A partial version of fold.

fun ref fold_partial[B: B](
  acc: B,
  f: {(B, A!): B^ ?}[A, B] box)
: B^ ?

Parameters

  • acc: B
  • f: {(B, A!): B^ ?}[A, B] box

Returns

  • B^ ?

last

[Source]

Return the last value of the iterator.

Example

Iter[I64]([1; 2; 3].values())
  .last()

3

fun ref last()
: A ?

Returns

  • A ?

map[B: B]

[Source]

Return an iterator where each item's value is the application of the given function to the value in the original iterator.

Example

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^

Parameters

  • f: {(A!): B^ ?}[A, B] box

Returns


nth

[Source]

Return the nth value of the iterator.

Example

Iter[I64]([1; 2; 3].values())
  .nth(2)

2

fun ref nth(
  n: USize val)
: A ?

Parameters

Returns

  • A ?

run

[Source]

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.

Example

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

Parameters

  • on_error: {ref()}[A] ref = lambda

Returns


skip

[Source]

Skip the first n values of the iterator.

Example

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^

Parameters

Returns


skip_while

[Source]

Skip values of the iterator while the predicate f returns true.

Example

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^

Parameters

  • f: {(A!): Bool ?}[A] box

Returns


take

[Source]

Return an iterator for the first n elements.

Example

Iter[I64]([1; 2; 3; 4; 5; 6].values())
  .take(3)

1 2 3

fun ref take(
  n: USize val)
: Iter[A] ref^

Parameters

Returns


take_while

[Source]

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.

Example

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^

Parameters

  • f: {(A!): Bool ?}[A] box

Returns


zip[B: B]

[Source]

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.

Example

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^

Parameters

Returns

  • Iter[(A , B)] ref^

zip2[B: B, C: C]

[Source]

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^

Parameters

Returns

  • Iter[(A , B , C)] ref^

zip3[B: B, C: C, D: D]

[Source]

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^

Parameters

Returns

  • Iter[(A , B , C , D)] ref^

zip4[B: B, C: C, D: D, E: E]

[Source]

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^

Parameters

Returns

  • Iter[(A , B , C , D , E)] ref^