I have a nested case class structure in List
for simplicity will use the following example:
case class Address(street: String, city: String, state: String, zipCode: Int) case class Person(firstName: String, lastName: String, addresses: List[Address]) case class Department(people: List[Person])
let's say that there is a List[Department] ; now if I want to create a new List[Department] by filtering Address for each Person that does not have a specific zipCode value; traditionally we can do the following
def filterPersonsByAddress(dlist: List[Department]): List[Department] = { dlist.map { d => val people = d.people.map { p => p.copy(addresses = p.addresses.filterNot(_.zipCode == 38978))} d.copy(people=people) } }
This approach does not work, because depending on the level of nesting, it can be large O (n ^ 2) or Big O (n ^ 3);
I am trying to learn lenses through Monocle . Until now, I have learned that lenses are useful when you need to โmodifyโ a deeply nested case class structure, but have not yet found a way to โchop offโ a certain part of the nested structure based on the condition and return a new structure. Is this possible for Monocle? Also, I'm not sure if lenses can help achieve a better Big O time?
source share