Skip to content

Instantly share code, notes, and snippets.

@jdegoes
Last active November 11, 2017 17:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdegoes/ce21e164b62bd0f4403e1fa76fd9051f to your computer and use it in GitHub Desktop.
Save jdegoes/ce21e164b62bd0f4403e1fa76fd9051f to your computer and use it in GitHub Desktop.
A sketch of an `Async ~> IO`
val AsyncToIO: NaturalTransformation[Async, IO] {
def apply[A](fa: Async[A]): IO[A] = {
for {
ref <- newIORef[Either[Throwable, A]](Left(new Error("No value")))
counter <- IO(new java.util.concurrent.CountDownLatch(1))
_ <- fa.register(v => ref.set(v).flatMap(_ => IO(counter.countDown()))
_ <- IO(counter.await())
v <- ref.get
a <- v match {
case Left(e) => IO.fail(e)
case Right(a) => IO(a)
}
} yield a
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment