![]() ![]() 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 |