soabase / soabase-halva

Idiomatic Scala ... in Java
https://github.com/soabase/soabase-halva/blob/master/README.md
Apache License 2.0
81 stars 5 forks source link

Should For.yield not return a Stream instead of a List? #13

Closed Alex-At-Home closed 8 years ago

Alex-At-Home commented 8 years ago

It's always easy to convert to a List at the end in the user code, but as soon as you internally Collectors.toList it you irretrievably lose the laziness.

Randgalt commented 8 years ago

I think, instead, there should be a stream() method or something to allow continuing the stream. yield() in Scala is a terminal.

Alex-At-Home commented 8 years ago

Oh you're totally right, I'm mixing up Haskell do and Scala for, d'oh :)

Alex-At-Home commented 8 years ago

Hmmm, are you sure yield is a terminal operation?

Eg http://docs.scala-lang.org/tutorials/FAQ/yield.html

    for(x <- c1; y <- c2; z <- c3) yield {...}
//===
    c1.flatMap(x => c2.flatMap(y => c3.map(z => {...})))

so if c1 is a stream then the for comprehension would also be a stream, no?

Randgalt commented 8 years ago

Scala doesn't have an analog to stream() right? You can directly map any sequence. I don't think there's a way to do this in Java without introducing a stream() method to For - which I think is a good idea actually.

Randgalt commented 8 years ago

So, with Halva, you'd end up with:

forComp(l, myList)
    .forComp(y, l.whatever)
    .stream(() -> i + y)    // stream() instead of yield
    .map(x -> x * 10)     // here we're out of forComp into JDK-land
Randgalt commented 8 years ago

stream() method added.