Created
May 9, 2013 19:15
-
-
Save globulon/5549794 to your computer and use it in GitHub Desktop.
Monad for input generator in fp in scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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