https://kotlinlang.org logo
#stdlib
Title
# stdlib
e

elizarov

03/06/2017, 8:30 AM
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

ilya.gorbunov

03/06/2017, 12:15 PM
elizarov: Interesting, how does it scale for nested immutable structures? 🤔
e

elizarov

03/06/2017, 3:35 PM
The slack channel is too small to explain it