Changes of state are wrapped in transactions, which ensure:
either all changes of a transaction are applied or none.
only valid changes are committed.
no transaction sees the effect of other transactions.
changes are persistent.
it's actually a causally-linked succession of immutable values created by applying a function to the current value.
an immutable magnitude, quantity, number, or composite of these
a series of causally related states over time
value of an identity at a moment in time
relative ordering of causal values.
Applying a function that modifies a data structure will return a new data structure rather than modifying the old one
=> (def a-map {:key "value"})
#'user/a-map
=> (assoc a-map :another-key "more values")
{:key "value", :another-key "more values"}
=> a-map
{:key "value"}
=> (def ^:dynamic a-var 42) #'user/a-var => a-var 42
=> (def ^:dynamic a-var 43) #'user/a-var => a-var 43
=> (defn print-var => ([] (println a-var)) => ([prefix] (println prefix a-var))) #'user/print-var => (bindind [a-var 44] => (print-var)) 44 nil
=> (import java.lang.Thread)
java.lang.Thread
=> (defn with-spawned-thread [fun]
=> (.start (java.lang.Thread. fun)))
#'user/with-spawned-thread
=> (do
=> (binding [a-var "rebound a-var"]
=> (with-spawned-thread
=> (fn [] (print-var "bg: ")))
=> (print-var "fg1: "))
=> (print-var "fg2: "))
bg: 42
fg1: rebound a-var
fg2: 42
nil
=> (def a-ref (ref 42))
#'user/a-ref
=> (ref-set a-ref 43)
IllegalStateException No transaction running clojure.lang.LockingTransaction.getEx (LockingTransaction.java:208)
=> (dosync (ref-set a-ref 43))
43
=> (deref a-ref)
43
=> @a-ref
43
=> (dosync (alter a-ref inc))
44
=> (def an-atom (atom 42))
#'user/an-atom
=> @an-atom
42
=> (swap! an-atom inc)
43
=> @an-atom
43
=> (swap! an-atom (fn [old] 44))
44
=> (reset! an-atom 42)
42
=> @an-atom
42
=> (def vector-atom (atom []))
#'user/vector-atom
=> (swap! vector-atom conj 42)
[42]
=> (swap! vector-atom conj 43)
[42 43]
=> (swap! vector-atom assoc-in [0] inc)
[43 43]
=> (def an-agent (agent 42))
#'user/an-agent
=> @an-agent
42
=> (send an-agent inc)
#<Agent@492f96c3: 43>