Lazy list evaluation, similar to Java Streams

Hello. ~2 years ago I tried Snap and I was clueless about many things - namely ringify. Now that I have discovered the beauty of lambda expressions, everything makes a lot more sense. :slight_smile: Anyways, because my college semester is over and I have nothing better to do, I have been experimenting with Snap and created this.

Now, I don't know the full mechanics of Java Streams, but I know intermediate operations are evaluated lazily (and not at all in the absence of a terminal operation). It was a headache to understand at first, but the whole thing with setting up the psuedo-call-stack/mega-nested-function (idk what to call it) reminds me of dominoes and I think it's pretty cool.

I know this forum is new (good timing on my part I guess) but I would love to hear feedback from you more experienced folk. Thanks!

Isn't lambda wonderful?

I don't know how Java Streams work, but I don't think it can be quite as you have it. In your implementation, no computation happens until the call to COLLECT, where it all happens at once -- just like regular all-at-once programming. The canonical example is to test whether a number is prime by making the range [2, n-1] as a stream, filtering out the ones that aren't factors, and reporting true iff the result is empty. And what doing this with streams is supposed to buy you is that if, for example, the number is even (as half of all integers are :slight_smile: ), as soon as filter finds that 2 is a factor, the FALSE is reported right away, not generating any more of the range [2, n-1], not testing any more possible factors, etc.

Here's my implementation:

The trick is that the IN FRONT OF STREAM block does not evaluate its second input, but instead implicitly wraps a lambda around it. Edit the block, click on "tail λ" then if you see something like this:
click on the arrowhead at the right edge. You'll then see this:

The "Any (unevaluated)" type means that the input slot looks like a regular input slot, but behaves like a "Reporter" type slot.

Absolutely, lambdas are quite cool.

Java has infinite streams, such as IntStream.iterate(0, t -> t + 1) to produce [0, 1, 2, 3, ...]. In typical Java fashion, stream support was manhandled into Collections with a bunch of utility classes/factory methods.

Just to restate what you said about "Any (unevaluated)", it is essentially a zero-argument lambda. I have used it once before to make a ternary operator.

Great code you have! Infinite streams shall be my next challenge. Interesting trick with "[] in front of []" - now this is turning into recursion. :open_mouth: It will take me a while to fully understand your code.

It's from Chapter 3 of SICP.

1 Like

what's a java stream?

It's a datatype in Java.
More about streams: Stream (computing) - Wikipedia
Java docs about streams: Stream (Java Platform SE 8 ) (

please quote only the section of the post you're talking about.

Read the code in the Snap! streams library.