Skip to content

Instantly share code, notes, and snippets.

@PavelZaytsev
Last active December 2, 2018 02:32
Show Gist options
  • Save PavelZaytsev/3be87e715d057d7015c63ffef319c921 to your computer and use it in GitHub Desktop.
Save PavelZaytsev/3be87e715d057d7015c63ffef319c921 to your computer and use it in GitHub Desktop.
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