Skip to content

Instantly share code, notes, and snippets.

@ThiporKong
Created May 14, 2013 14:58
Show Gist options
  • Save ThiporKong/5576580 to your computer and use it in GitHub Desktop.
Save ThiporKong/5576580 to your computer and use it in GitHub Desktop.
trait Run[F[_]] {
def apply[I](request: F[I]): (I, Run[F])
def unit[I](i: I): F[I] // needed to be able to implement Monad[F]
}
def runnerMonad[F[_]](requestRunner: Run[F]) = new Monad[F] {
// keep the current state of the runner here
var runner = requestRunner
def unit[A](a: => A): F[A] = runner.unit(a)
def flatMap[A, B](ma: F[A])(f: A => F[B]): F[B] = {
runner.apply(ma) match { case (a, ma1) => runner = ma1; f(a) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment