Lenses if a functional solution to this problem. N...
# stdlib
e
Lenses if a functional solution to this problem. No, in imperative language you can do better. Just like with collections. How would you modify an an immutable collection efficiently? Imagine something like this:
Copy code
val list = … // some immutable list here
val updated = list.mutate { 
   add(”…”) // efficiently perform a series of mutating operations
   add(”…”)
}
where
mutate
is a helper function that “opens” and efficiently rebuilds immutable structure:
Copy code
fun <T> List<T>.mutate(block: MutableList<T>.() -> Unit): List<T>
You should have a similar API for immutable data classes:
Copy code
val person = Person(first = “John”, last = “Smith”) // immutable person object with lots of props
val updated = person.mutate { 
   age = 31
   occuptation = “Manager”
}
It is considerably more readable than the solution with lenses.
i
elizarov: Interesting, how does it scale for nested immutable structures? 🤔
e
The slack channel is too small to explain it