Skip to content

Instantly share code, notes, and snippets.

@globulon
Created May 9, 2013 19:15
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 globulon/5549794 to your computer and use it in GitHub Desktop.
Save globulon/5549794 to your computer and use it in GitHub Desktop.
Monad for input generator in fp in scala
trait Runnable[A] {
def run: A
}
object Delay {
def apply[A](a: => A) = new Runnable[A] {
def run: A = a
}
def map[A, B](r: Runnable[A])(f: A => B): Runnable[B] = new Runnable[B] {
def run = f(r.run)
}
def flatMap[A, B](r: Runnable[A])(f: A => Runnable[B]): Runnable[B] = new Runnable[B] {
def run = f(r.run).run
}
val monad: Monad[Runnable] = new Monad[Runnable] {
def flatMap[A, B](ma: Runnable[A])(f: (A) => Runnable[B]) = Delay.flatMap(ma)(f)
override def map[A, B](ma: Runnable[A])(f: (A) => B) = Delay.map(ma)(f)
def unit[A](a: => A) = Delay(a)
}
}
//where you can run something like
object RunIO {
def main(args: Array[String]) {
val F = IO.monad[Runnable]
import F._
val s = sequence(List.fill(10000)(IO {math.random}).toList)
println(IO.run(Delay.monad)(s).run)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment