Created
July 4, 2015 09:31
-
-
Save julien-truffaut/a28eef85a17188411de8 to your computer and use it in GitHub Desktop.
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
import monocle.function._ | |
import monocle.std.list._ | |
import monocle.Lens | |
import monocle.syntax.apply._ | |
case class Person(name: String, age: Int) | |
val ps = List(Person("John", 25), Person("Paul", 32)) | |
val _age = Lens[Person, Int](_.age)(a => p => p.copy(age = a)) | |
// or val _age = GenLens[Person](_.age) | |
(ps applyOptional index(1) composeLens _age).set(10) == List(Person("John", 25), Person("Paul", 10)) | |
// we only need the apply syntax to guide type inference in the choice of index instance | |
// if instead you collection is inside of an object we would not need it | |
case class Familly(name: String, persons: List[Person]) | |
val _persons = Lens[Familly, List[Person]](_.persons)(p => f => f.copy(persons = p)) | |
(_persons composeOptional index(1) composeLens _age) | |
.set(10)(Familly("Doe", ps)) == Familly("Doe", List(Person("John", 25), Person("Paul", 10))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment