Created
April 4, 2014 07:09
-
-
Save milessabin/9969589 to your computer and use it in GitHub Desktop.
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
scala> :paste | |
// Entering paste mode (ctrl-D to finish) | |
case class Address(street : String, city : String, postcode : String) | |
case class Person(name : String, age : Int, address : Address) | |
// Exiting paste mode, now interpreting. | |
defined class Address | |
defined class Person | |
scala> import Lens._ | |
import Lens._ | |
scala> val ageLens = Lens[Person] >> 'age | |
ageLens: shapeless.Lens[Person,Int]{val gen: shapeless.LabelledGeneric[Person]{type Repr = shapeless.::[shapeless.record.FieldType[shapeless.tag.@@[Symbol,String("name")],String],shapeless.::[shapeless.record.FieldType[shapeless.tag.@@[Symbol,String("age")],Int],shapeless.::[shapeless.record.FieldType[shapeless.tag.@@[Symbol,String("address")],Address],shapeless.HNil]]]}} = shapeless.Lens$$anon$6@6eee179a | |
scala> val cityLens = Lens[Person] >> 'address >> 'city | |
cityLens: shapeless.Lens[Person,String]{val gen: shapeless.LabelledGeneric[Address]{type Repr = shapeless.::[shapeless.record.FieldType[shapeless.tag.@@[Symbol,String("street")],String],shapeless.::[shapeless.record.FieldType[shapeless.tag.@@[Symbol,String("city")],String],shapeless.::[shapeless.record.FieldType[shapeless.tag.@@[Symbol,String("postcode")],String],shapeless.HNil]]]}} = shapeless.Lens$$anon$6@30eb50db | |
scala> val person = Person("Joe Grey", 37, Address("Southover Street", "Brighton", "BN2 9UA")) | |
person: Person = Person(Joe Grey,37,Address(Southover Street,Brighton,BN2 9UA)) | |
scala> ageLens.get(person) | |
res0: Int = 37 | |
scala> ageLens.modify(person)(_+1) | |
res1: Person = Person(Joe Grey,38,Address(Southover Street,Brighton,BN2 9UA)) | |
scala> cityLens.get(person) | |
res2: String = Brighton | |
scala> cityLens.set(person)("London") | |
res3: Person = Person(Joe Grey,37,Address(Southover Street,London,BN2 9UA)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment