Skip to content

Instantly share code, notes, and snippets.

@optician
Created November 26, 2018 13:12
Show Gist options
  • Save optician/5eb886bf45d5ed6ecf760f5b2c30a9fe to your computer and use it in GitHub Desktop.
Save optician/5eb886bf45d5ed6ecf760f5b2c30a9fe to your computer and use it in GitHub Desktop.
import cats.effect.concurrent.Ref
import cats.effect.{IO, Sync}
import cats._
import cats.implicits._
import fs2.Stream
def g(r: => IO[Ref[IO, Int]]): Stream[IO, Int] =
Stream
.eval(r)
.flatMap(
x =>
Stream.eval(for {
i <- x.get
_ <- x.set(i + 1)
} yield i)
)
def f: Stream[IO, Int] = {
val j = Ref.of[IO, Int](1)
val r = for {
xs <- IO(g(j))
ys <- IO(g(j))
} yield xs ++ ys
Stream.eval(r).flatten
}
f.map(println).compile.drain.unsafeRunSync()
// Result:
// 1
// 1
// Desire:
// 1
// 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment