Skip to content

Instantly share code, notes, and snippets.

@freewind
Forked from Centaur/lens.scala
Created October 21, 2015 16:21
Show Gist options
  • Save freewind/8ff86a00175bbe914e64 to your computer and use it in GitHub Desktop.
Save freewind/8ff86a00175bbe914e64 to your computer and use it in GitHub Desktop.
Nested Lens
import monocle.macros.Lenses
import language.higherKinds
@Lenses("_") case class Street(name: String)
@Lenses("_") case class Address(street: Street)
@Lenses("_") case class Company(addresses: Seq[Address])
@Lenses("_") case class Employee(company: Company)
object ILens {
val employee = Employee(Company(Seq(
Address(Street("aaa string")),
Address(Street("bbb string")),
Address(Street("bpp string")))))
import Employee._
import Company._
import Address._
import Street._
def main(args: Array[String]) {
def transformName(name: String) = if(name.startsWith("b")) name.toUpperCase else name
def singleAddr(address: Address) = (_street composeLens _name).modify(transformName)(address)
val converted = (_company composeLens _addresses).modify(_.map(singleAddr))(employee)
println(converted)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment