Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.