Skip to content

Instantly share code, notes, and snippets.

@gvolpe
Created May 28, 2018 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gvolpe/5419820e04ae74fc4335ada598384d79 to your computer and use it in GitHub Desktop.
Save gvolpe/5419820e04ae74fc4335ada598384d79 to your computer and use it in GitHub Desktop.
def scanEval[F[_]: Sync, S, A](p: Stream[F, A])(start: F[S])(f: (S, A) => F[S]): Stream[F, S] = {
def zipper(ref: Ref[F, S]): Stream[F, S] =
p.zip(Stream.eval(ref.get).repeat).evalMap { case (a, s) =>
for {
ns <- f(s, a)
_ <- ref.set(ns)
} yield ns
}
for {
st <- Stream.eval(start)
ref <- Stream.eval(Ref.of[F, S](st))
rs <- zipper(ref)
} yield rs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment