Say I have a atom initially set to 0. I would like to initialize it only if it is not previously initialized,
What I have now:
(swap! atom #(if % % (initialize)))
However, this just doesn't look idiomatic to me.
There must be a more readable way right?
Often you can use fnil
to avoid needing to do the initialization check in the first place. For instance (swap! counts update word (fnil inc 0))
is saying increment the value at key word in counts, but if there is no value there use 0.
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