Created
November 26, 2018 13:12
-
-
Save optician/5eb886bf45d5ed6ecf760f5b2c30a9fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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