Last active
September 8, 2016 12:34
-
-
Save labra/21a57dcb4d26443ed5591bd871f136be to your computer and use it in GitHub Desktop.
Example using eff-cats localReader
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
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