Skip to content

Instantly share code, notes, and snippets.

@vmarquez
Last active August 29, 2015 14:16
Show Gist options
  • Save vmarquez/bac38b6605359124bb83 to your computer and use it in GitHub Desktop.
Save vmarquez/bac38b6605359124bb83 to your computer and use it in GitHub Desktop.
object Test {
import scalaz.{Lens,State}
case class BlahConfig(server: Int, l: List[Int])
def doStuff(i: Int) = State[BlahConfig,Int]( c => (c.copy(l = i :: c.l), c.server) )
case class FooConfig(val password: String)
def doSomeStuff(i: Int) = State[FooConfig, String](c =>(c, c.password))
//CONCCRETE CONFIG
case class RConfig(s: Int, p: String, l: List[Int])
//A way to go from a 'smaller' type to a bigger type
val l1 = Lens.lensu[RConfig, BlahConfig]( (a,b) => a.copy(s = b.server, l = b.l), b => BlahConfig(server=b.s, l = b.l))
val l2 = Lens.lensu[RConfig, FooConfig]( (a,b) => a.copy(p = b.password), b => FooConfig(password = b.p))
val res = for {
a <- doStuff(2).zoom(l1)
b <- doSomeStuff(a).zoom(l2)
} yield a + b
res(RConfig(1, "asdf", List(1)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment