Skip to content

Instantly share code, notes, and snippets.

@raspasov
Last active August 29, 2015 14:11
Show Gist options
  • Save raspasov/7f89bc69884fea298468 to your computer and use it in GitHub Desktop.
Save raspasov/7f89bc69884fea298468 to your computer and use it in GitHub Desktop.
Clojure STM operations
(defn new-ops-collector
"Create a new op collector
The collector is a vector of vectors, each internal vector
is a tuple of f and vector-of-params, i.e. [f vector-of-params].
Later on we execute all the ops in a single transaction via (execute-ops collector)
Example structure: [[f [param1 param2 etc]] etc ] "
[]
(atom []))
(defn add-op
"Schedule an op to be executed in a transaction"
[ops-collector f params]
(swap! ops-collector conj [f params]))
(defn execute-ops
"Execute all accumulated ops in a single STM transaction"
[ops-collector]
(dosync (doseq [[^IFn f ^APersistentVector params] @ops-collector] (apply f params))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment