Atomically alters the root binding of var v by applying f to its
current value plus any args
(defn sqr [n]
"Squares a number"
(* n n))
user=> (sqr 5)
25
user=> (alter-var-root
(var sqr) ; var to alter
(fn [f] ; fn to apply to the var's value
#(do (println "Squaring" %) ; returns a new fn wrapping old fn
(f %))))
user=> (sqr 5)
Squaring 5
25
(defn alter-var-root
"Atomically alters the root binding of var v by applying f to its
current value plus any args"
{:added "1.0"}
[^clojure.lang.Var v f & args] (.alterRoot v f args))
Comments top
No comments for alter-var-root. Log in to add a comment.