Closed Alex-At-Home closed 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.
Oh you're totally right, I'm mixing up Haskell do
and Scala for
, d'oh :)
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?
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.
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
stream() method added.
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.