Iterator Reference
Iterator is an abstract base class that all other iterator classes inherit from.
var iterator = [0, 1, 2].iter();Methods
Section titled “Methods”Returns true if all values match the predicate, otherwise false.
var is_even = [2, 4, 6].iter().all((value) -> value % 2 == 0); // trueParameters
Section titled “Parameters”predicate: (dyn!) -> Boolean - The predicate that each value must be true for.
Returns
Section titled “Returns”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); // trueParameters
Section titled “Parameters”predicate: (dyn!) -> Boolean - The predicate that each value could be true for.
Returns
Section titled “Returns”Boolean - true if any value matches the predicate, otherwise false.
Combines two iterators.
var iter = Range(0, 3).chain([3, 4].iter());Parameters
Section titled “Parameters”other: Iterator - The other iterator to add.
Returns
Section titled “Returns”ChainIterator - A ChainIterator combining the two iterators.
collect
Section titled “collect”Collects an iterator to an array.
var arr = Range(0, 3).collect(); // [0, 1, 2]Parameters
Section titled “Parameters”None
Returns
Section titled “Returns”[dyn!] - An array of each item in the iterator.
Counts the items in an iterator.
var count = Range(0, 3).count(); // 3Parameters
Section titled “Parameters”None
Returns
Section titled “Returns”Number - The number of items in the iterator.
enumerate
Section titled “enumerate”Enumerates the iterator, zipping it with incrementing numbers.
var iter = ["first", "second", "third"].iter().enumerate();Parameters
Section titled “Parameters”start: Number? optional - The number to start enumerating with. Defaults to 0.
Returns
Section titled “Returns”EnumerateIterator - An EnumerateIterator enumerating the iterator.
has_next
Section titled “has_next”var iter = [“first”, “second”, “third”.iter().has_next(); // true
Parameters
Section titled “Parameters”None
Returns
Section titled “Returns”Boolean - Returns true if the iterator has more items, otherwise false.
filter
Section titled “filter”Filters an iterator by a predicate.
var iter = ["first", "second", "third"].iter().filter((word) -> word.length() > 5);Parameters
Section titled “Parameters”predicate: (dyn!) -> Boolean - The predicate to filter the iterator by.
Returns
Section titled “Returns”FilterIterator - A FilterIterator for filtering the iterator.
filter_indexed
Section titled “filter_indexed”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);Parameters
Section titled “Parameters”predicate: (Number, dyn!) -> Boolean - The transform function where the first parameter is the index, and the second parameter is the element of the array.
Returns
Section titled “Returns”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);Parameters
Section titled “Parameters”predicate: (dyn!) -> Boolean - The predicate to filter the iterator by.
Returns
Section titled “Returns”dyn? - The value if found, otherwise nil.
flat_map
Section titled “flat_map”Transforms each item of the iterator, returning a new iterator.
var iter = [0, 1, 2].iter().flat_map((value) -> Range(0, value));Parameters
Section titled “Parameters”transform: (dyn!) -> Iterator - The transform function.
Returns
Section titled “Returns”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); // 6Parameters
Section titled “Parameters”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.
Returns
Section titled “Returns”dyn? - The final accumulated value.
for_each
Section titled “for_each”Iterates over each element in the iterator.
Range(0, 3).for_each(println);// 0// 1// 2Parameters
Section titled “Parameters”consumer: (dyn!) -> nil - A function to call on each item in the iterator.
Returns
Section titled “Returns”nil
for_each_indexed
Section titled “for_each_indexed”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"Parameters
Section titled “Parameters”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.
Returns
Section titled “Returns”nil
Iterates over each element and transforms them.
Range(0, 3).map((value) -> value + 1);Parameters
Section titled “Parameters”transform: (dyn!) -> dyn! - A function to call on each item in the iterator to transform it.
Returns
Section titled “Returns”MapIterator - A MapIterator for transforming items in an iterator.
map_indexed
Section titled “map_indexed”Iterates over each element and transforms them.
Range(0, 3).map_indexed((i, value) -> i + value);Parameters
Section titled “Parameters”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.
Returns
Section titled “Returns”MapIndexedIterator - A MapIndexedIterator for transforming items in an iterator.
var iter = [“first”, “second”, “third”].iter().next(); // “first”
Parameters
Section titled “Parameters”None
Returns
Section titled “Returns”dyn! - The next item in the iterator.
reduce
Section titled “reduce”Iterates over an iterator, updates an accumlator, then returns it.
var iter = [1, 2, 3].iter().reduce((acc, value) -> acc + value); // 6Parameters
Section titled “Parameters”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.
Returns
Section titled “Returns”dyn? - The final accumulated value.
reverse
Section titled “reverse”Collects and reverses the items in an iterator.
var iter = [1, 2, 3].iter().reduce((acc, value) -> acc + value); // 6Parameters
Section titled “Parameters”None
Returns
Section titled “Returns”ArrayIterator - The iterator with its items reversed.
Skips a number of items in the iterator.
var iter = Range(0, 10).skip(5);Parameters
Section titled “Parameters”limit: Number - How many items to skip.
Returns
Section titled “Returns”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);Parameters
Section titled “Parameters”limit: Number - How many items to take.
Returns
Section titled “Returns”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));Parameters
Section titled “Parameters”other: Iterator - The other iterator to zip with.
Returns
Section titled “Returns”ZipIterator - A ZipIterator for zipping two iterators together into an iterator of Pairs.
Example
Section titled “Example”var even_numbers = Range(0, 10).iter() .filter((value) -> value % 2 == 0) .map((value) -> to_string(value)) .collect(); // ["0", "2", "4", "6", "8"]