Skip to content

Instantly share code, notes, and snippets.

@labra
Last active September 8, 2016 12:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save labra/21a57dcb4d26443ed5591bd871f136be to your computer and use it in GitHub Desktop.
Save labra/21a57dcb4d26443ed5591bd871f136be to your computer and use it in GitHub Desktop.
Example using eff-cats localReader
package examples
import cats._, data._
import org.atnos.eff._, all._
import org.atnos.eff.syntax.all._
import cats.implicits._
object ExampleLocalReader {
def exampleInterpreter = {
type Env = Map[String,Int]
type ReaderEnv[A] = Reader[Env, A]
type Comp = Fx.fx2[ReaderEnv, Option]
def lookup(x: String): Eff[Comp,Int] = for {
e <- ask[Comp,Env]
v <- OptionEffect.fromOption[Comp,Int](e.get(x))
} yield v
def runLocal[A](f:Env => Env, c: Eff[Comp,A]): Eff[Comp,A] =
c.modifyReader(f)
def program: Eff[Comp,String] = for {
v <- runLocal(_.updated("x",2), lookup("x"))
e <- ask[Comp,Env]
} yield s"Value: $v, env: $e"
val env: Env = Map()
program.runReader(env).runOption.run
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment