Skip to content

Iterator Reference

Iterator is an abstract base class that all other iterator classes inherit from.

var iterator = [0, 1, 2].iter();

Returns true if all values match the predicate, otherwise false.

var is_even = [2, 4, 6].iter().all((value) -> value % 2 == 0); // true

predicate: (dyn!) -> Boolean - The predicate that each value must be true for.

Boolean - true if every value matches the predicate, otherwise false.

Returns true if any of the values match the predicate, otherwise false.

var is_at_least_one_even = [1, 4, 6].iter().any((value) -> value % 2 == 0); // true

predicate: (dyn!) -> Boolean - The predicate that each value could be true for.

Boolean - true if any value matches the predicate, otherwise false.

Combines two iterators.

var iter = Range(0, 3).chain([3, 4].iter());

other: Iterator - The other iterator to add.

ChainIterator - A ChainIterator combining the two iterators.

Collects an iterator to an array.

var arr = Range(0, 3).collect(); // [0, 1, 2]

None

[dyn!] - An array of each item in the iterator.

Counts the items in an iterator.

var count = Range(0, 3).count(); // 3

None

Number - The number of items in the iterator.

Enumerates the iterator, zipping it with incrementing numbers.

var iter = ["first", "second", "third"].iter().enumerate();

start: Number? optional - The number to start enumerating with. Defaults to 0.

EnumerateIterator - An EnumerateIterator enumerating the iterator.

var iter = [“first”, “second”, “third”.iter().has_next(); // true

None

Boolean - Returns true if the iterator has more items, otherwise false.

Filters an iterator by a predicate.

var iter = ["first", "second", "third"].iter().filter((word) -> word.length() > 5);

predicate: (dyn!) -> Boolean - The predicate to filter the iterator by.

FilterIterator - A FilterIterator for filtering the iterator.

Filters an iterator by a predicate and provides an index.

var iter = ["first", "second", "third"].iter().filter_indexed((i, word) -> word.length() > 5 or i == 0);

predicate: (Number, dyn!) -> Boolean - The transform function where the first parameter is the index, and the second parameter is the element of the array.

FilterIndexedIterator - A FilterIndexedIterator for filtering the iterator.

Finds an item in an iterator by a predicate.

var iter = ["first", "second", "third"].iter().find((word) -> word.length() == 6);

predicate: (dyn!) -> Boolean - The predicate to filter the iterator by.

dyn? - The value if found, otherwise nil.

Transforms each item of the iterator, returning a new iterator.

var iter = [0, 1, 2].iter().flat_map((value) -> Range(0, value));

transform: (dyn!) -> Iterator - The transform function.

FlatMapIterator - A FlatMapIterator for wrapping items in an iterator with an iterator.

Iterates over an iterator, updates an accumlator, then returns it.

var iter = [1, 2, 3].iter().fold(0, (acc, value) -> acc + value); // 6

initial: dyn! - The initial accumulator value for the fold.

reduce_function: (dyn!, dyn!) -> dyn! - A function that takes in the current accumlator and current value, and returns the next accumulator.

dyn? - The final accumulated value.

Iterates over each element in the iterator.

Range(0, 3).for_each(println);
// 0
// 1
// 2

consumer: (dyn!) -> nil - A function to call on each item in the iterator.

nil

Iterates over each element in the iterator.

Range(0, 3).for_each_indexed((i, value) -> {
println("Number " + (i + 1) + ": " + to_string(value));
});
// "Number 1: 0"
// "Number 2: 1"
// "Number 3: 2"

consumer: (Number, dyn!) -> nil - A function to call on each item in the iterator. The first paramter is the index of the item, and the second is the item in the iterator.

nil

Iterates over each element and transforms them.

Range(0, 3).map((value) -> value + 1);

transform: (dyn!) -> dyn! - A function to call on each item in the iterator to transform it.

MapIterator - A MapIterator for transforming items in an iterator.

Iterates over each element and transforms them.

Range(0, 3).map_indexed((i, value) -> i + value);

transform: (Number, dyn!) -> dyn! - A function to call on each item in the iterator to transform it. The first parameter is an index, and the second parameter is the item at that index.

MapIndexedIterator - A MapIndexedIterator for transforming items in an iterator.

var iter = [“first”, “second”, “third”].iter().next(); // “first”

None

dyn! - The next item in the iterator.

Iterates over an iterator, updates an accumlator, then returns it.

var iter = [1, 2, 3].iter().reduce((acc, value) -> acc + value); // 6

reduce_function: (dyn!, dyn!) -> dyn! - A function that takes in the current accumlator and current value, and returns the next accumulator. The first accumulator value is the first element of the iterator.

dyn? - The final accumulated value.

Collects and reverses the items in an iterator.

var iter = [1, 2, 3].iter().reduce((acc, value) -> acc + value); // 6

None

ArrayIterator - The iterator with its items reversed.

Skips a number of items in the iterator.

var iter = Range(0, 10).skip(5);

limit: Number - How many items to skip.

An Iterator with first provided number of items skipped.

Takes a number of items from the beginning of the iterator.

var iter = Range(0, 10).take(5);

limit: Number - How many items to take.

TakeIterator - A TakeIterator for taking the provided number of items.

Zips two iterators together into an iterator of Pairs.

var iter = Range(0, 5).zip(Range(5, 10));

other: Iterator - The other iterator to zip with.

ZipIterator - A ZipIterator for zipping two iterators together into an iterator of Pairs.

var even_numbers = Range(0, 10).iter()
.filter((value) -> value % 2 == 0)
.map((value) -> to_string(value))
.collect(); // ["0", "2", "4", "6", "8"]