Clojure の deref 可能な値
(deref ref)、@ref
deref - clojure.core | ClojureDocs - Community-Powered Clojure Documentation and Examples
@ref/@agent/@var/@atom/@delay/@future/@promise
暗黙に変化し得る参照の現在の値を取得する
Var
Clojure - Vars and the Global Environment
(var n)、#'n
var - clojure.core | ClojureDocs - Community-Powered Clojure Documentation and Examples
Clojure - Special#var Forms
set!、var-set で値を変える
可変な名を指示する
Ref、Agent、Atom、Volatile はその参照自体は無名
Ref
Clojure - Refs and Transactions
(ref v)
ref - clojure.core | ClojureDocs - Community-Powered Clojure Documentation and Examples
dosync 内で値を変える。alter や ref-set や commute で値を変える
STM (software transactional memory) を実現する
Agent
Clojure - Agents and Asynchronous Actions
(agent v)
agent - clojure.core | ClojureDocs - Community-Powered Clojure Documentation and Examples
send で値を変える
Agent 用の thread pool が有り、Agent 毎に異なる thread で値が管理される
ElixirElixir.iconに移入された
Agent - The Elixir programming language
Agent — Elixir v1.13.4
Atom
Clojure - Atoms
(atom v)
atom - clojure.core | ClojureDocs - Community-Powered Clojure Documentation and Examples
swap!、reset! で値を変える
dosync で複雑な transaction を作らなくて済む用途の簡易な Ref (実装は独立している)
thread safe な Volatile
Volatile
(volatile! v)
volatile! - clojure.core | ClojureDocs - Community-Powered Clojure Documentation and Examples
vswap!、vreset! で値を変える
non-thread safe な Atom
全く thread safe でない。concurrency を一切考慮しない時に使う
JavaJava.iconの volatile 修飾子とは關係無いよ
遅延処理用
Delay
(delay v)
delay - clojure.core | ClojureDocs - Community-Powered Clojure Documentation and Examples
deref するまで実行せず、deref すると同期的に実行し結果を返す。結果は cache される
Future
(future v)
future - clojure.core | ClojureDocs - Community-Powered Clojure Documentation and Examples
即座に parallel に中身を実行する。deref すると処理が終わる迄 block し結果を返す。結果は cache される
Promise
(promise)
promise - clojure.core | ClojureDocs - Community-Powered Clojure Documentation and Examples
promise p を deref すると (deliver p v) されるまで block しその値を返す。結果は cache される
code:promise.clj
(let p (promise)
(future (Thread/sleep 1000)
(deliver p 42))
@p)