Clojureのatom
from Clojure
非協調的、同期的な変更の管理
他のatomの値とは関係なく更新できる
これを非協調的と呼んでる
Clojureのrefよりも軽量
https://japan-clojurians.github.io/clojure-site-ja/reference/atoms
https://gist.github.com/kohyama/6076544#atom
atom
atomを作成
deref,@
atomにアクセス
swap!
atomの値の変更
swap!(a, fn)
atomであるaに1引数関数fnを適用してaを更新する
reset!
例
code:mal(lisp)
user> (def! atm (atom (list 0 1 2 3 4 5 6 7 8 9)))
(atom (0 1 2 3 4 5 6 7 8 9))
user> (first @atm)
0
user> @atm
(0 1 2 3 4 5 6 7 8 9)
user> @atm
(0 1 2 3 4 5 6 7 8 9)
user> (swap! atm (fn* a (concat (rest a) (list (first a)))))
(1 2 3 4 5 6 7 8 9 0)
user> (swap! atm (fn* a (concat (rest a) (list (first a)))))
(2 3 4 5 6 7 8 9 0 1)
user> (swap! atm (fn* a (concat (rest a) (list (first a)))))
(3 4 5 6 7 8 9 0 1 2)
user> (deref atm)
(3 4 5 6 7 8 9 0 1 2)
#??
普通の変数とは何が違うの
immutabelであること?
どんな値でも追加できるの?
いけるっぽい
code:clojure
(def! a (atom (fn* (a) a)))
((deref a) 10) ; 10