Reputation: 2181
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?
Upvotes: 5
Views: 460
Reputation: 10672
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.
Upvotes: 7