How to convert Stream[Either[A,B]] to Either[A,Stream[B]] and still be lazy


I am trying to use streams to delay memory and CPU usage. Initially I tried to use Stream.foldLeft to perform the transformation indicated in the header. I realized that the fold consumes all elements in the stream. After posting
and deleting a question regarding this I now and realizing that in fact it does make sense that fold traverse the
whole stream.

My question is basically this: is it possible to perform the transformation so the the resulting type contains
a lazy stream (without traversing the all elements)?


I guess you want to get a Left when any of the elements of the Stream is a Left? In that case you don’t have much of a choice but to evaluate the whole Stream up to the first Left, or to the end if there is no Left.

1 Like


I finally realized this. Unfortunately I could not delete the topic.

Thanks @Jasper-M.

Would converting to Either[Option[A], Stream[B]] be useful?

Not really. That Left is simply used to signal an Error.