Is there an easy way in Ceylon to create an infinite iterable which generates each element by calling the same given no-args function? In other words, does the language module offer an equivalent to Java 8's Stream.generate(Supplier<T>)?
Here's what I came up with:
{Value+} generator<Value>(Value() generate) => {generate()}.cycled;
This works because {generate()} is lazy.
No this doesn't actually exist right now and I think the "Ceylonish" way would be something like this:
class Generator<T>(T func()) satisfies Iterable<T> {
object iter satisfies Iterator<T> {
next() => func();
}
iterator() => iter;
}
Also you could open a request for it on: the language module
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With