Skip to content

Instantly share code, notes, and snippets.

@nrinaudo
Created August 13, 2018 21:17
Show Gist options
  • Save nrinaudo/3d708658432c04a48a549633f55091fd to your computer and use it in GitHub Desktop.
Save nrinaudo/3d708658432c04a48a549633f55091fd to your computer and use it in GitHub Desktop.
Demo of how Reader works
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
}
@phderome
Copy link

phderome commented Aug 15, 2018

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment