Modify nested sealed hierarchies & enums: import ._Ĭase class Fish(name: String) extends PetĬase class Cat(name: String) extends LeggedPetĬase class Dog(name: String) extends LeggedPetįish("Finn"), Cat("Catia"), Dog("Douglas") Val p6 = (modifyAddress andThenModify modifyStreetName).using(_.toUpperCase)(person) Val modifyStreetName = modifyLens(_.street.name) Val modifyAddress = modifyLens(_.address) Val p6 = (modifyAddress andThenModify modifyStreetName)(person).using(_.toUpperCase) Val modifyStreetName = modify(_: Address)(_.street.name) Val modifyAddress = modify(_: Person)(_.address) Val upperCaseStreetName = modifyLens(_.).using(_.toUpperCase)Ĭomposing lenses: import ._ Val p4 = ing(_.toLowerCase)(anotherPerson) Val upperCaseStreetName = modify(_: Person)(_.).using(_.toUpperCase)Īlternate syntax: import ._ Val p4 = modifyStreetName(anotherPerson).using(_.toLowerCase) Val p3 = modifyStreetName(person).using(_.toUpperCase) Val modifyStreetName = modify(_: Person)(_.) Re-usable modifications (lenses): import ._ Modify fields when they are of a certain subtype: trait AnimalĬase class Cat(ages: Seq) extends Animal Val prodResource = devResource.modify(_.).setTo("real") Val devResource = Resource(auth = Left(AuthContext("fake")) eachRight: case class AuthContext(token: String)Ĭase class Resource(auth: Either) atOrElse is currently not available for sequences because quicklens might need to insert manyĮlements in the list in order to ensure that one is available at a particular position, and it's notĬlear that providing one default for all keys is the right behavior. person.modify(_.addresses.at(2).street.atOrElse(Street("main street")).name).using(_.toUpperCase) atOrElse takes no arguments and acts similarly. atOrElse will make one using the second parameterĪnd perform subsequent modifications on the newly instantiated default.įor Options. If props contains an entry for "NumReports", then. atOrElse: personWithProps.modify(_.props.atOrElse("NumReports", Property("0")).value).setTo("5") Modify specific elements in an option or map with a fallback using. index modifies only the element with the given key. PersonWithProps.modify(_.props.index("Age").value).setTo("45") index works for map keys as well: case class Property(value: String) index selects the lone possible element in street: Option. index(2) selects an element in addresses: ListĪnd. index modifies only the element at the given index/key. index: person.modify(_.addresses.index(2).).using(_.toUpperCase) Modify specific elements in an option/sequence/map using. at modifies only the element with the given key. PersonWithProps.modify(_.props.at("Age").value).setTo("45") at works for map keys as well: case class Property(value: String)Ĭase class PersonWithProps(name: String, props: Map) at selects the lone possible element in street: Option. at(2) selects an element in addresses: ListĪnd. If there's no element at that index,Īn IndexOutOfBoundsException is thrown. at modifies only the element at the given index/key. at: person.modify(_.addresses.at(2).street.at.name).using(_.toUpperCase) street.eachWhere(_.name.startsWith("1")).name) modify(_.addresses.eachWhere(filterAddress) def filterAddress: Address => Boolean = ? eachWhere(p) where p is a predicate to modify only the elements which satisfy You can add support for your own containers by providing an implicit QuicklensFunctor with the appropriate Mapss - only values are unwrapped for maps). each can only be used inside a modify and "unwraps" the container (currently supports Seqs, Options and Val p2 = person.modify(_.name).using(_.toUpperCase) each: import ._Ĭase class Address(street: Option)Ĭase class Person(addresses: List)Īddress(Some(Street("1 Functional Rd."))),Īddress(Some(Street("2 Imperative Dr."))) Person.modifyAll(_.firstName, _.middleName.each, _.lastName).using(_.capitalize) Val person = Person("john", Some("steve"), "smith") Modify several fields in one go: import ._Ĭase class Person(firstName: String, middleName: Option, lastName: String) Person.modify(_.).setToIf(shouldChangeAddress)("3 00 Ln.") Modify conditionally: person.modify(_.).setToIfDefined(Some("3 00 Ln.")) Val p4 = modify(person)(_.).using(_.toUpperCase) Val p3 = person.modify(_.).setTo("3 OO Ln.") Val p2 = person.modify(_.).using(_.toUpperCase) Val person = Person(Address(Street("1 Functional Rd.")), 35) Modify deeply nested fields in case classes: import ._Ĭase class Person(address: Address, age: Int)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |