Last active
December 2, 2018 02:32
-
-
Save PavelZaytsev/3be87e715d057d7015c63ffef319c921 to your computer and use it in GitHub Desktop.
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 IO[A] { | |
def run: A // case class parameters may not be call-by-name | |
// so we put unit in a singleton | |
} | |
object IO { | |
def pure[A](input: => A): IO[A] = new IO[A] { def run: A = input } | |
def >==>[A, B, C](a: A => IO[B], b: B => IO[C]): A => IO[C] = { input => | |
val pureIntput: IO[A] = pure(input) | |
val pureB: IO[B] = flatMap(pureIntput)(a) | |
flatMap(pureB)(b) | |
} | |
// again in terms of flatMap | |
def map[A, B](a: IO[A])(f: A => B): IO[B] = { | |
flatMap(a) { input => | |
pure(f(input)) | |
} | |
} | |
def flatMap[A, B](a: IO[A])(f: A => IO[B]): IO[B] = { | |
flatMap(a)(f) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment