Created
August 13, 2018 21:17
-
-
Save nrinaudo/3d708658432c04a48a549633f55091fd to your computer and use it in GitHub Desktop.
Demo of how Reader works
This file contains hidden or 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
import cats._ | |
import cats.instances.all._ | |
import cats.syntax.all._ | |
final case class Configuration() | |
final case class Database() | |
final case class HttpServer() | |
object Test extends App { | |
def initLog(): Configuration ⇒ Unit = ??? | |
def initDatabase(): Configuration ⇒ Database = ??? | |
def initHttp(db: Database): Configuration ⇒ HttpServer = ??? | |
val initAll: Configuration ⇒ HttpServer = for { | |
_ ← initLog() | |
db ← initDatabase() | |
http ← initHttp(db) | |
} yield http | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To complete this we'd need the following no? Or you are using something I missed and Cats picks up Reader implicits automatically?
import cats.data.Reader
final case class Configuration()
final case class Database()
final case class HttpServer()
object Test extends App {
def initLog(): Configuration ⇒ Unit = c => ()
def initDatabase(): Configuration ⇒ Database = c => Database()
def initHttp(db: Database): Configuration ⇒ HttpServer = c => HttpServer()
val initAll: Reader[Configuration, HttpServer] = for {
_ ← Reader(initLog())
db ← Reader(initDatabase())
http ← Reader(initHttp(db))
} yield http
val http = initAll.run(Configuration())
// do something with http
}