Import std.core.iterator
Iterator
interface
Public Interface Iterator<T>
What a For loop goes over: each Next gives the next value, and its adapters and consumers work on the values that are left.
Examples
Struct Countdown Implements Iterator<Int>
Field At: Int
Public ByRef Method Next(): Int?
If At == 0 Then Return Null
At -= 1
Return At + 1
End Method
End Struct
Local seen: Int[] = []
For n In Countdown(3)
seen.Add(n)
End For
Expect(seen == [3, 2, 1])
Expect(Countdown(5).Filter(Function(n: Int) => n % 2 == 1).ToArray() == [5, 3, 1])-
The next value, or Null when there are no more.
-
What
changegives for each value. -
The values
keepsays to keep. -
The first
countvalues, or all of them when there are fewer.Examples
Expect([1, 2, 3, 4].Iterate().Skip(1).Take(2).ToArray() == [2, 3]) -
The values after the first
count. -
The values before the first that
keepsays not to keep. -
The values from the first that
skipsays not to skip. -
Each value with how many came before it.
Examples
Expect(["a", "b"].Iterate().Enumerate().ToArray() == [(0, "a"), (1, "b")]) -
Each value with
other's at the same place, as far as the shorter goes.Examples
Expect([1, 2, 3].Iterate().Zip(["a", "b"].Iterate()).ToArray() == [(1, "a"), (2, "b")]) -
These values, then
other's. -
The values of each value, which is an
Iterable<U>, one after another. -
The first value, then each step-th one after it.
-
The values in arrays of
size, the last one shorter when they don't divide evenly.Examples
Expect([1, 2, 3, 4, 5].Iterate().Chunks(2).ToArray() == [[1, 2], [3, 4], [5]]) -
Each run of
sizevalues in a row, in an array of its own: none when there are fewer.Examples
Expect([1, 2, 3, 4].Iterate().Windows(3).ToArray() == [[1, 2, 3], [2, 3, 4]]) -
The values, smallest
keyfirst, where equal keys keep their order. It goes over them all when it's first asked for one.Examples
Local words = ["pear", "fig", "apple"].Iterate() Local shortest = words.OrderBy(Function(word: String) => word.Length) Expect(shortest.ToArray() == ["fig", "pear", "apple"]) -
The values, largest
keyfirst, where equal keys keep their order. -
The values, in a new array.
-
How many values there are.
-
Whether
testsays yes for any value, going no further than the first. -
Whether
testsays yes for every value, going no further than the first it doesn't. -
The first value
testsays yes for, or Null. -
The first value, or Null when there's none.
-
The last value, or Null when there's none.
-
What
combinemakes ofstartand each value in turn. -
What
combinemakes of the values, the first two, then that and the next, and so on: the first alone when there's one, and Null when there's none. -
Runs
actionfor each value. -
The values added up, or zero when there are none.
Examples
Expect([1, 2, 3].Iterate().Sum() == 6) Expect([2, 3, 4].Iterate().Product() == 24) -
The values multiplied together, or one when there are none.
-
The smallest value, the first of equal ones, or Null when there's none.
-
The largest value, the first of equal ones, or Null when there's none.
-
The value whose key is smallest, the first of equal ones, or Null when there's none.
-
The value whose key is largest, the first of equal ones, or Null when there's none.
Examples
Local words = ["pear", "fig", "apple"].Iterate() Expect(words.MaxBy(Function(word: String) => word.Length) == "apple")
Report an issue with this page or with FutureBlitz on GitHub.