Last active
October 29, 2015 07:39
-
-
Save strobe/ae7c16350a3932dd580b to your computer and use it in GitHub Desktop.
monocle automatically generated lenses example
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
package monocle | |
import monocle.macros.{GenLens, Lenses} | |
import shapeless.test.illTyped | |
class LensMonoExample extends MonocleSuite { | |
// @Lenses generate Lens automatically in the companion object | |
@Lenses case class Address(streetNumber: Int, streetName: String) | |
@Lenses case class Person(name: String, age: Int, address: Address) | |
val john = Person("John", 30, Address(126, "High Street")) | |
test("get") { | |
Person.name.get(john) shouldEqual "John" | |
} | |
test("set") { | |
val changedJohn = john.copy(age = 45) | |
Person.age.set(45)(john) shouldEqual changedJohn | |
} | |
test("compose") { | |
(Person.address composeLens Address.streetNumber).get(john) shouldEqual 126 | |
} | |
test("Modifications through lenses are chainable") { | |
@Lenses case class Point(x: Int, y: Int) | |
import Point._ | |
val update = x.modify(_ + 100) compose y.set(7) | |
update(Point(1,2)) shouldEqual Point(101,7) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment