swap!

clojure.core

  • (swap! atom f)
  • (swap! atom f x)
  • (swap! atom f x y)
  • (swap! atom f x y & args)
Atomically swaps the value of atom to be:
(apply f current-value-of-atom args). Note that f may be called
multiple times, and thus should be free of side effects. Returns
the value that was swapped in.

2 Examples top

  • user> (def players (atom ()))
    #'user/players
    
    user> (swap! players conj :player1)
    (:player1)
    
    user> (swap! players conj :player2)
    (:player2 :player1)
    
    user> (deref players)
    (:player2 :player1)
  • user> (def counter (atom 0))
    #'user/counter
    
    user> (swap! counter inc)
    1
    
    user> (swap! counter inc)
    2
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:2099 top

(defn swap!
  "Atomically swaps the value of atom to be:
  (apply f current-value-of-atom args). Note that f may be called
  multiple times, and thus should be free of side effects.  Returns
  the value that was swapped in."
  {:added "1.0"
   :static true}
  ([^clojure.lang.Atom atom f] (.swap atom f))
  ([^clojure.lang.Atom atom f x] (.swap atom f x))
  ([^clojure.lang.Atom atom f x y] (.swap atom f x y))
  ([^clojure.lang.Atom atom f x y & args] (.swap atom f x y args)))
Vars in clojure.core/swap!:
Used in 0 other vars

Comments top

No comments for swap!. Log in to add a comment.