I want to append to a Stream
But next stream relies on previous Stream
's folding result
Here is how I did, but the Stream s
is evaluated twice
import fs2._
def ints(start: Int) = Stream.iterate(start) { i =>
println(i)
i + 1
}.take(10)
val s = ints(0)
def foldAppend(init: Int)(f: (Int, Int) => Int)(next: Int => Stream[Pure, Int]) = {
s ++ s.fold(init)(f).flatMap(next)
}
val res = foldAppend(0)((s, i) => s + 1)(ints)
println(res.toList)
How can I implement the foldAppend
method that evalute s
only once.
Brian's answer is wrong, s
is actually lazy so the entire stream is evaluated twice. The variable binding to s
is strict, but Stream
in fs2 is a lazy stream, that is only evaluated once you run
it.
Your main issue is that Pure
is not a monad for implementing side effect safely, like IO
. You shouldn't println
in pure. An example that works is:
import cats.effect.IO
import fs2._
def ints(start: Int) = Stream.iterate(start) { i => println(i)
i + 1
}.take(10)
val s = ints(0)
def foldAppend(init: Int)(f: (Int, Int) => Int)(next: Int => Stream[IO, Int]) = {
val result = s.covary[IO].runLog
Stream.eval(result).covary[IO].flatMap {
s =>
Stream.emits(s) ++ Stream.emits(s).fold(init)(f).flatMap(next)
}
}
val res = foldAppend(0)((s, i) => s + 1)(ints)
println(res.runLast.unsafeRunSync())
This will evaluate the stream once
Finally get work done with Pull
implicit class StreamSyntax[F[_], A](s: Stream[F, A]) {
def foldAppend[S](init: S)(f: (S, A) => S)(next: S => Stream[F, A]): Stream[F, A] = {
def pullAll(s: Stream[F, A]): Pull[F, A, Option[(Chunk[A], Stream[F, A])]] = {
s.pull.unconsChunk.flatMap {
case Some((hd, tl)) =>
Pull.output(hd) *> pullAll(tl)
case None =>
Pull.pure(None)
}
}
def foldChunks(i: S, s: Stream[F, A]): Pull[F, A, Option[(Chunk[A], Stream[F, A])]] = {
s.pull.unconsChunk.flatMap {
case Some((hd, tl)) =>
val sum: S = hd.toVector.foldLeft(i)(f)
Pull.output(hd) *> foldChunks(sum, tl)
case None =>
pullAll(next(i))
}
}
foldChunks(init, s).stream
}
}
Have you considered using scala.collection.immutable.Stream
? It's cached, so it won't be evaluated multiple times.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.