Function Subspace :: sus :: iter :: from_generator
template <class F, int &... , class R = std::invoke_result_t<F &&>>
auto from_generator(F&& f) -> Iterator<R::Item> auto
requires
sus::fn::FnOnce<F, ::sus::fn::NonVoid (void)>
__private::IsGenerator<R>::value
Produces an iterator over Item
from a coroutine function that returns
Generator<Item>
and yields Item
s.
This is just a syntactic aid, as Generator<Item>
is an iterator, so
calling the generator function is what produces the iterator.
Example
auto generate_fibonacci = []() -> Generator<i32> {
co_yield 0;
i32 n1 = 0, n2 = 1;
while (true) {
i32 next = n1 + n2;
n1 = n2;
n2 = next;
co_yield n1;
}
};
// Directly using the generator iterator, in a for loop.
sus::Vec<i32> v;
for (i32 i : generate_fibonacci().take(7u)) {
v.push(i);
}
sus_check(v == sus::Vec<i32>(0, 1, 1, 2, 3, 5, 8));
// Using `from_generator`, with collect.
sus::Vec<i32> v2 = generate_fibonacci().take(7u).collect_vec();
sus_check(v2 == sus::Vec<i32>(0, 1, 1, 2, 3, 5, 8));