I have a number of atoms in my code where a common requirement is to update them to a new value, regardless of the current value.
I therefore find myself writing something like this:
(swap! atom-name (fn [_] (identity new-value)))
This works but seems pretty ugly and presumably incurs a performance penalty for constructing the anonymous closure.
Is there a better way?
The reset!
function should do this.
(reset! atom-name new-value)
You can use (compare-and-set atom old-value new-value).
But I find it strange you need to change them so much to uncorrelated values. Can't you use bindings or similar things.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With