Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinite iterable generator in Ceylon

Tags:

ceylon

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>)?

like image 206
gdejohn Avatar asked Dec 06 '25 00:12

gdejohn


2 Answers

Here's what I came up with:

{Value+} generator<Value>(Value() generate) => {generate()}.cycled;

This works because {generate()} is lazy.

like image 97
gdejohn Avatar answered Dec 08 '25 17:12

gdejohn


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

like image 45
Quintesse Avatar answered Dec 08 '25 18:12

Quintesse