Just for testing purposes I wanted to compute lazily 2 elements:
Stream(
{ Thread.sleep(2000); 1 },
{ Thread.sleep(2000); 2 },
Stream.empty[Int]
).foreach(println)
But running this code does not yield the desired result. The values seem to show up all at the same time in the output. The reason is that the Stream()
constructor is taking an array that has to be computed eagerly. So to fix that, I had to resort to manually creating the thunks of the stream like this:
(
{ Thread.sleep(2000); 1 } #::
{ Thread.sleep(2000); 2 } #::
Stream.empty[Int]
).foreach(println)
which now works exactly as intended but is not particularly pretty.
Is there a cleaner and more convenient way of having something syntactically similar to Stream(a, b, c)
but that evaluates the arguments lazily?
Thanks