elizarov
03/06/2017, 8:30 AMval 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:
fun <T> List<T>.mutate(block: MutableList<T>.() -> Unit): List<T>
You should have a similar API for immutable data classes:
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.ilya.gorbunov
03/06/2017, 12:15 PMelizarov
03/06/2017, 3:35 PM