Created
November 8, 2018 04:27
-
-
Save arunma/04231897f087fd8f54f18ad531c4b582 to your computer and use it in GitHub Desktop.
Cats WriterMonad wrapping a Future
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
def getCurrentTemperatureW(): WriterT[Future, List[String], Double] = { | |
WriterT.putT(Future.successful(10.0))(List("Thermometer isn't broken yet")) | |
} | |
def getTomorrowsTempFromPredictionAPIW(curr: Double): WriterT[Future, List[String], Double] = { | |
WriterT.putT(Future.successful(20.0))(List("Yay, the Prediction API works too")) | |
} | |
def publishItInOurWebsiteW(pred: Double): WriterT[Future, List[String], Double] = { | |
WriterT.putT(Future.successful(20.0))(List("Published to our website")) | |
} | |
val publishedWriter = | |
for { | |
curr <- getCurrentTemperatureW() | |
pred <- getTomorrowsTempFromPredictionAPIW(curr) | |
pub <- publishItInOurWebsiteW(pred) | |
} yield pub | |
val futureRun = publishedWriter.run | |
val (logs, value) = Await.result(futureRun, 2 seconds) | |
println(logs.mkString("\n")) | |
println(s"\nValue is $value") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment