开发者

Updating an atom with a single value

I have a number of atoms in my code where a common requirement is to update them to a new value, regardless of 开发者_Python百科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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜