Skip to content

Instantly share code, notes, and snippets.

@danslapman
Created October 16, 2016 15:07
Show Gist options
  • Save danslapman/f9011969f0b67bbc2528ec7b266295de to your computer and use it in GitHub Desktop.
Save danslapman/f9011969f0b67bbc2528ec7b266295de to your computer and use it in GitHub Desktop.
StateT as a monad
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
import scalaz.{Monad, StateT}
import scalaz.std.AllInstances._
object Test extends App {
implicit val ifsmi = StateT.stateTMonadState[Int, Future]
import ifsmi.{get, put}
val algo = for {
_ <- ifsmi.whileM_(get map (_ < 10),
for {
i <- get
_ <- put(i + 1)
} yield ()
)
v1 <- get
} yield v1
println(Await.result(algo.run(0), 10.seconds))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment