Last active
February 1, 2019 12:46
-
-
Save yasuabe/6f01bde84d9e687debbfd4a462884247 to your computer and use it in GitHub Desktop.
Cats Effect Async Demo
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
package cats_effect_typeclass_exericise.async | |
import java.time.format.DateTimeFormatter | |
import cats.data.IorT | |
import cats.effect._ | |
import cats.instances.string._ | |
import scala.concurrent.Future | |
import scala.language.higherKinds | |
object Util { | |
val formatter = DateTimeFormatter.ofPattern("mm:ss") | |
def putIn(milli: Int, s: Any): String = { | |
Thread.sleep(milli) | |
val threadId = Thread.currentThread.getId | |
val currentMinSec = java.time.LocalTime.now.format(formatter) | |
println(s"in thread($threadId) at $currentMinSec): $s") | |
s.toString | |
} | |
def putLine(s: Any): String = putIn(1000, s) | |
def putIn1Sec(s: Any): String = putIn(1000, s) | |
def putNow(s: Any): String = putIn(0, s) | |
def putRnd(s: Any): String = putIn((math.random * 1000 + 500).toInt, s) | |
} | |
import Util._ | |
object AsyncAsyncDemoApp extends IOApp { | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.util.{Failure, Success} | |
val apiCall: Future[String] = Future.successful { | |
putLine("in successful block") | |
"the future" | |
//} // (A)...Success Case | |
} map { _ => throw new Exception("ERROR")} // (B)...Failure Case | |
def run(args: List[String]): IO[ExitCode] = { | |
val ioa: IO[String] = Async[IO] async { cb => | |
putIn1Sec("begin async block") | |
apiCall onComplete { | |
case s @ Success(value) => putIn1Sec(s"completed: $s"); cb(Right(value)) | |
case e @ Failure(th) => putIn1Sec(s"failed: $e"); cb(Left(th)) | |
} | |
putIn1Sec("end async block") | |
} | |
putIn1Sec("ioa initialized") | |
ioa redeem[ExitCode] ( | |
th => { putNow(th.getMessage); ExitCode.Error }, | |
s => { putNow(s); ExitCode.Success } | |
) | |
} | |
//---- (A) Success Case | |
// in thread(1) at 20:44): in successful block | |
// in thread(1) at 20:45): ioa initialized | |
// in thread(11) at 20:46): begin async block | |
// in thread(11) at 20:47): end async block | |
// in thread(13) at 20:48): completed: Success(the future) | |
// in thread(13) at 20:48): the future | |
// | |
// Process finished with exit code 0 | |
//---- (B) Failure Case | |
// in thread(1) at 24:53): in successful block | |
// in thread(1) at 24:54): ioa initialized | |
// in thread(11) at 24:56): begin async block | |
// in thread(11) at 24:57): end async block | |
// in thread(13) at 24:57): failed: Failure(java.lang.Exception: ERROR) | |
// in thread(13) at 24:57): ERROR | |
// | |
// Process finished with exit code 1 | |
} | |
object AsyncIorTDemoApp extends IOApp { | |
type StringIorT[T] = IorT[IO, String, T] | |
def right1234(implicit I: Async[StringIorT]): StringIorT[Int] = { | |
I async { cb => | |
putLine(s"sleeping for 3 sec") | |
Thread.sleep(3000) | |
cb(Right(1234)) | |
} | |
} | |
def run(args: List[String]): IO[ExitCode] = { | |
val both: StringIorT[Int] = right1234.flatMap(n => IorT.bothT("left", n)) | |
both.value map { s => | |
putLine(s) | |
ExitCode.Success | |
} | |
} | |
} | |
// in thread(11) at 46:51): sleeping for 3 sec | |
// in thread(11) at 46:54): Both(left,1234) | |
// | |
// Process finished with exit code 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment