by Scarbutt · 4y ago · 0 comments
(update-fn (fn [{:keys [address]}
:as customer]
(if address (update customer :address clojure.string/capitalize) customer))
customers)
which pattern matches on some `object` (map) and does processing. We find it less fragile than specifying explicit path to an element. It can also work in a polymorphic fashion. On the other hand, there is a risk of a false positive (when you modify address that you shouldn't). But you can mitigate that risk by using additional checks (in case of a customer, you can check for additional set of fields that are specific for that object(map)
edit:formatting (update-vals customers #(update-in % [:address 0] str/capitalize))Most good FP data structures will be tree like, but with a low branching factor and chunky leafs. Now of course that does not save you from lots of memory consumption if you use a language/platform where even primitives are boxed, like the JVM. But that is a completely separate topic.
To be clear it is certainly a solvable problem (as this post shows) but it can make things difficult.
The point is neither copying the whole structure nor diffs are as easy as mutex + inner mutation.
Although I don't know what you mean by growing lenses.
Mutex + inner mutation is no easier than CAS (which is the usual solution with concurrent writing of immutable data structures) a la Java AtomicReference or STM (another popular one) and in my opinion significantly harder as soon as you have multiple mutexes.
It's very costly though.
Under the hood it's a effectively a single CAS instruction that loops on failure (which only occurs under contention, but then you have waiting with locks too).