Skip to content

Instantly share code, notes, and snippets.

@animatedlew
Created June 16, 2017 04:31
Show Gist options
  • Save animatedlew/5f7d9acd718287bff1b8e04a692fc070 to your computer and use it in GitHub Desktop.
Save animatedlew/5f7d9acd718287bff1b8e04a692fc070 to your computer and use it in GitHub Desktop.
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Source
import cats.data.State
object Main extends App {
implicit val system = ActorSystem("akka")
implicit val ec = system.dispatcher
implicit val materializer = ActorMaterializer()
// dumb state monad
val s = State[String, Int] {
case "world-state" => ("success", 1)
case _ => ("blah", 0)
}
val done = Source(1 to 10)
.map { d =>
if (d == 10) for (z <- s) yield z + d // xform the value using comprehensions
else s
}
.map { d => d.modify { case "success" => "awesome" } } // xform the state using set/get/modify
.map { _.run("world-state").value } // run the monad xformer get retrieve the value
.runForeach(println)
done
.map { d => println(d) }
.map { _ => system.terminate() }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment