3

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

gdejohn
  • 7,451
  • 1
  • 33
  • 49

2 Answers2

5

Here's what I came up with:

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

This works because {generate()} is lazy.

gdejohn
  • 7,451
  • 1
  • 33
  • 49
1

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

Quintesse
  • 452
  • 2
  • 9
  • [The FAQ says it is called "ceylonic"](http://ceylon-lang.org/documentation/1.1/faq/#am_i_allowed_to_say_ceylonish). – Paŭlo Ebermann Sep 07 '15 at 17:57
  • That was [added almost a year after](https://github.com/ceylon/ceylon-lang.org/commit/94f311119d1e50971794be225a77c18ed9b220cc) I answered that question ;) – Quintesse Sep 08 '15 at 20:06