Skip to content

Instantly share code, notes, and snippets.

@zhangxu
Last active August 29, 2015 14:01
Show Gist options
  • Save zhangxu/40a717e03e2abe123dac to your computer and use it in GitHub Desktop.
Save zhangxu/40a717e03e2abe123dac to your computer and use it in GitHub Desktop.
Scalaz Reader of Configuration
import scalaz.Reader
case class Configuration(hostname: String, port: Int, file: String)
case class ServerConnection(host: String, port: Int, file: String)
val config = Configuration("localhost", 8080, "index.html")
def configReader[A](f: Configuration => A): Reader[Configuration, A] = Reader { f }
val conn: scalaz.Kleisli[scalaz.Id.Id, Configuration, ServerConnection] = for {
h <- configReader { _.hostname }
p <- configReader { _.port }
o <- configReader { _.file }
} yield ServerConnection(h, p, o)
val serverConn = conn.run(config) // conn(config)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment