By Alvin Alexander. Last updated: June 4, 2016
When you first start working with immutable values and functional programming (FP), you quickly wonder, “If I can’t mutate a variable value, how do I, well, mutate it? (What can I do?)”
The answer is that you don’t mutate it; you update the fields you need to modify as you copy the old value to a new value. This is okay for simple objects, but when you have deeply nested objects, i.e., cases classes inside of case classes inside of case classes, your code will look like the second part of this image.
Without getting too much into the problem, the solution is something that FP devs call a lens. This image comes from this article that discusses scalaz lenses (and I show a Quicklens example at this URL).