Skip to content

Instantly share code, notes, and snippets.

@maxkorolev
Created July 11, 2019 07:14
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 maxkorolev/2f9c6a2ac4be9d16df9e2a674fb5c449 to your computer and use it in GitHub Desktop.
Save maxkorolev/2f9c6a2ac4be9d16df9e2a674fb5c449 to your computer and use it in GitHub Desktop.
Naive Reader
case class Fun[A, B](run: A => B) {
def map[C](f: B => C): Fun[A, C] = Fun(a => f(run(a)))
def flatMap[C](f: B => Fun[A, C]): Fun[A, C] =
Fun(a => f(run(a)).run(a))
}
case class Config(db: String, logColor: String)
object Repo {
def get(id: Int): Fun[Config, Int] = Fun(conf => id)
}
object Logger {
def log(value: Int): Fun[Config, Unit] =
Fun(conf => println(s"$RESET${conf.logColor}$value$RESET"))
}
val r = for {
a <- Repo.get(123)
_ <- Logger.log(a)
b <- Repo.get(123)
_ <- Logger.log(b)
s = a + b
_ <- Logger.log(s)
} yield s
val conf = Config("", GREEN)
r.run(conf) must beEqualTo(246)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment