Skip to content

Instantly share code, notes, and snippets.

@arunma
Created November 8, 2018 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arunma/04231897f087fd8f54f18ad531c4b582 to your computer and use it in GitHub Desktop.
Save arunma/04231897f087fd8f54f18ad531c4b582 to your computer and use it in GitHub Desktop.
Cats WriterMonad wrapping a Future
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