Import std.core.iterator
std.core.iterator
Iterators, and the Iterable Interface, which For loops go over besides arrays.
Each Next gives the next element, or Null when there are no more. The loop goes over a copy of a Struct, since its Next changes it.
Its adapters are lazy: each gives an Iterator of its own, which asks the one before it for a
value only when it's asked for one. Its consumers go over the values once. They're all Final,
so they're what every Iterator has, through its value too. Called on a Struct, they go over a
copy of it, and a Class's object is used up, as a For loop over it is. A @PanicsAtCaller Next
panics where a consumer is called, and through an adapter or a box, where its panic is, as
through any Iterator<T> value.
Examples
// Anything with an Iterate() is Iterable, which a For loop goes over.
Struct Countdown Implements Iterable<Int>
Field From: Int
Public Method Iterate(): Range<Int> => From To 1 Step -1
End Struct
Local seen: Int[] = []
For n In Countdown(3)
seen.Add(n)
End For
Expect(seen == [3, 2, 1])
Expect(Empty<Int>().Count() == 0)std/core/iterator.fb
Types
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")
Iterable
interface
Public Interface Iterable<T>
What a For loop goes over through its Iterate(), for code that takes any collection. A
collection's Iterate() gives a Struct of its own, which an Iterable<T>'s value or generic code
gets in a box, an Iterator<T> object.
std/core/iterator.fb, line 410
-
Its values, from the first, in an Iterator of their own.
Functions
Repeated
function
@PanicsAtCaller Public Function Repeated<T>(value: T, count: Int): Iterator<T>
value, count times.
Examples
Expect(Repeated("ab", 3).ToArray() == ["ab", "ab", "ab"])
Empty
function
Public Function Empty<T>(): Iterator<T>
No values.
Report an issue with this page or with FutureBlitz on GitHub.