Value patterns: do vs. collect

Hi all, I just encountered something that has me puzzled. I’m aware that you can use .collect on a pattern to modify its output:

p = Pwhite(1, 9).collect(_.squared).asStream.nextN(10);

And I had assumed that .do would behave similarly, except that it would return its unmodified receiver, instead of a modified result. However, the following seems to crash the interpreter, and I see nothing in the post window.

p = Pwhite(1, 9).do(_.squared).asStream.nextN(10);

Does anyone know what I’m overlooking or misunderstanding?


With Pwhite(1, 9).do(...) you make an infinite loop. It doesn’t crash the interpreter but locks it up.

Unlike collect(), do is not “lazy”. (collect called on a pattern returns a new pattern Pcollect, but do really tries to run over all the elements in the Pwhite).

Makes perfect sense, thanks. I did not realize one was lazy and the other wasn’t!