Last active
November 11, 2017 17:02
-
-
Save jdegoes/ce21e164b62bd0f4403e1fa76fd9051f to your computer and use it in GitHub Desktop.
A sketch of an `Async ~> IO`
This file contains hidden or 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
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