Created
October 21, 2015 15:05
-
-
Save Centaur/8fe066c605d37aa5f6fe to your computer and use it in GitHub Desktop.
Nested Lens
This file contains 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
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
Version that need not modify
Seq
toList
in case class definitions: