package core.iter
as_iter :: (list: &List) -> #autoas_iter :: (s: &Set) -> #autoas_iter :: (b: &Bucket_Array($T)) -> Iterator(T)as_iter :: (arr: [] $T) -> #auto
where type_is_struct(T)as_iter :: (arr: [] $T, by_pointer: bool) -> #autoas_iter :: (arr: [] $T) -> #autoas_iter :: (arr: [] $T) -> #auto
where type_is_struct(T)as_iter :: (arr: [] $T, by_pointer: bool) -> #autoas_iter :: (arr: [] $T) -> #autoas_iter :: macro (x: &[..] $T) -> #autoas_iter :: macro (x: &[..] $T, by_pointer: bool) -> #autoas_iter :: (r: range) -> Iterator(i32)as_iter :: (r: range64) -> Iterator(i64)as_iter :: (x: &$T) -> #auto
where ImplicitIterator(T)as_iter :: macro (x: $T) -> #auto
where HasAsIter(T)The standard function to convert something to an Iterator. For-loops currently do not use this function to determine how to iterate over something unknown, but that could be a feature down the line.
close :: (it: Iterator) -> voidHelper function to close an iterator, if a close function is defined.
collect :: (it: Iterator($T), allocator) -> [..] TCollects elements into an array, or a map, depending on if the iterator produces a Pair(K, V) or not.
comp :: macro (i: Iterator(&$V), value: Code) -> #autocomp :: macro (i: Iterator($V), value: Code) -> #autocomp :: macro (i: $I, value: Code) -> #auto
where Iterable(I)Simple iterator comprehensions, in the same vein as Pythons comprehension syntax.
Python:
results = [it * 2 for it in [1, 2, 3, 4, 5]]
Onyx:
results := iter.comp(u32.[1, 2, 3, 4, 5], [it](it * 2));
concat :: (iters: ..Iterator($T)) -> Iterator(T)Combines iterators by first yielding all values from one, then yielding all values from the next, and so on.
count :: macro (it: $T, cond: $F) -> #auto
where Iterable(T)count :: (it: Iterator($T), cond: (T) -> bool) -> i32 Returns how many times the cond was true.
counter :: (start: type, $type: type_expr) -> Iterator(type)Helper function to create an infinite counting iterator.
Use start to configure the starting value.
Use type to configure the type used for the iterator.
distributor :: macro (it: $T) -> #auto
where Iterable(T)distributor :: (it: Iterator) -> Iterator(it.Iter_Type)empty :: ($T: type_expr) -> Iterator(T)Helper function to create an iterator of a type that does not produce an values.
enumerate :: macro (it: $T, start_index: i32) -> #auto
where Iterable(T)enumerate :: (it: Iterator($T), start_index: i32) -> Iterator(CALL)Yields a value that contains:
1) the value from the iterator,
2) an incrementing integer.
every :: macro (it: $T, cond: $F) -> #auto
where Iterable(T)every :: (it: Iterator($T), cond: (T) -> bool) -> bool Returns if cond returned true for all yielded values.
filter :: (it: Iterator($T), predicate: (T) -> bool) -> #autofilter :: (it: Iterator($T), ctx: $Ctx, predicate: (T, Ctx) -> bool) -> #autoOnly yields the values for which the predicate is true.
find :: macro (it: $T, predicate: $F) -> #auto
where Iterable(T)find :: (it: Iterator($T), predicate: (T) -> bool) -> ? Tflat_map :: (it: Iterator($T), transform: (T) -> Iterator($R)) -> #autoflat_map :: (it: Iterator($T), ctx: $Ctx, transform: (T, Ctx) -> Iterator($R)) -> #autoTransforms every value that comes out of an iterator using the transform function into a new iterator, from which subsequent values will be output.
iter.flat_map(iter.as_iter(1 .. 5), x => iter.as_iter(1 .. x+1))
// 1, 1, 2, 1, 2, 3, 1, 2, 3, 4
flatten :: (i: Iterator($T), f: (T) -> ? $R) -> Iterator(R)Filters and maps at the same time.
If the provided function returns a None variant of Optional, then the entry is discarded.
If the provided function returns Some(x), then x is yielded.
fold :: macro (it: $T, init: $R, combine: $S) -> #auto
where Iterable(T)fold :: (it: Iterator($T), initial_value: $R, combine: (T, R) -> R) -> R Incremently calls combine on the yielded value and the accumulated value, producing a new accumulated value. Returns the final accumulated value.
fold1 :: macro (it: $T, combine: $S) -> #auto
where Iterable(T)fold1 :: (it: Iterator($T), combine: (T, T) -> T) -> ? T Incremently calls combine on the yielded value and the accumulated value, producing a new accumulated value. Returns the final accumulated value.
from_array :: (arr: [] $T) -> #auto
where type_is_struct(T)from_array :: (arr: [] $T, by_pointer: bool) -> #autofrom_array :: (arr: [] $T) -> #auto from_array has two almost identical implementations, but the details are important here. Normally, from_array returns an iterator by value, unless the array is of structures, then it returns an iterator by pointer. This seems weird, but in practice it is closer to what you want, as you don't want to have to copy every structure out of the array. While for primitives, you don't want to dereference it everywhere.
generator :: (ctx: &$Ctx, gen: (&Ctx) -> ? $T) -> Iterator(T)generator :: (ctx: &$Ctx, gen: (&Ctx) -> ? $T, close: (&Ctx) -> void) -> Iterator(T)Using the polymorph solving system, you can write type free versions of arbitrary iterators. This is used heavily by many of the functions defined above.
Maybe at some point an alternate allocator would be good for this? For now, I think the temporary allocator is sufficient.
generator_no_copy :: (ctx: &$Ctx, gen: (&Ctx) -> ? $T) -> #autogenerator_no_copy :: (ctx: &$Ctx, gen: (&Ctx) -> ? $T, close: (&Ctx) -> void) -> #autogroup_by :: (it: Iterator($T), cmp: (T, T) -> bool) -> Iterator(CALL) Groups like elements together using the provided comparison function. cmp should return true if the two elements are equal. The items should be sorted in such a way that the equal items appear next to each other.
map :: (it: Iterator($T), transform: (T) -> $R) -> #automap :: (it: Iterator($T), ctx: $Ctx, transform: (T, Ctx) -> $R) -> #autoTransforms every value that comes out of an iterator using the transform function.
next :: (it: Iterator) -> ? it.Iter_TypeHelper function to get the next value out of an iterator.
next_opt :: (it: Iterator) -> ? it.Iter_Type Helper function to get the next value out of an iterator, but translated to an optional. Returns None if the iterator was empty, Some(value) otherwise.
parallel_for :: macro (iterable: $I, thread_count: u32, thread_data: &$Ctx, body: Code) -> void
where Iterable(I)parallel_for :: macro (iter: Iterator($T), thread_count: u32, thread_data: &$Ctx, body: Code) -> voidAllows you to easily write a parallelized for-loop over an iterator. For example,
iter.parallel_for(1 .. 100, 4, &.{}) {
printf("Thread {} has {}!\n", context.thread_id, it);
}
prod :: macro (x: $I, y: $I2) -> #auto
where Iterable(I), Iterable(I2)prod :: (x: $I1, y_iter: Iterator($Y)) -> #auto
where Iterable(I1)Produces an iterator that first yields all values from the first iterable, combined with the first yield value from the second iterable. Then, steps the second iterable, and repeats.
For example,
iter.prod(1 .. 4, 1 .. 3)
Would yield:
(1, 1), (2, 1), (3, 1), (1, 2), (2, 2), (3, 2)
scan :: macro (it: $T, init: $R, combine: $S) -> #auto
where Iterable(T)scan :: (it: Iterator($T), initial_value: $R, combine: (T, R) -> R) -> Iterator(R)scan1 :: macro (it: $T, combine: $S) -> #auto
where Iterable(T)scan1 :: (it: Iterator($T), combine: (T, T) -> T) -> Iterator(T)skip :: (it: Iterator($T), count: u32) -> Iterator(T) Discards the first count values and yields all remaining values.
skip_while :: (it: Iterator($T), predicate: (T) -> bool) -> Iterator(T)skip_while :: (it: Iterator($T), ctx: $Ctx, predicate: (T, Ctx) -> bool) -> Iterator(T)Discards values while the predicate is true, then yields all values.
some :: macro (it: $T, cond: $F) -> #auto
where Iterable(T)some :: (it: Iterator($T), cond: (T) -> bool) -> bool Returns if cond returned true for any yielded value.
sum :: macro (it: $T) -> #auto
where Iterable(T)sum :: (it: Iterator($T)) -> T Returns the sum of all yield values, using the + operator.
take :: (it: Iterator($T), count: u32) -> Iterator(T) Only yields the first count values, then closes.
take_while :: (it: Iterator($T), predicate: (T) -> bool) -> Iterator(T)Yields values while the predicate returns true.
to_array :: (it: Iterator($T), allocator) -> [..] TPlaces all yielded values into a dynamically allocated array, using the allocator provided (context.allocator by default).
to_map :: (it: Iterator(CALL), allocator) -> Map(K, V) Places all yielded values into a Map, with the first member being the key, and the second member being the value.