You're viewing version 1.2.0 of alter. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

alter

clojure.core

  • (alter ref fun & args)
Must be called in a transaction. Sets the in-transaction-value of
ref to:

(apply fun in-transaction-value-of-ref args)

and returns the in-transaction-value of ref.

1 Example top

  • ;; alter is a way to change the value of a reference.
    
    ;; Here we're defining a ref named 'names' and setting its value to
    ;; an empty vector.
    user=> (def names (ref []))
    #'user/names
    
    ;; A function to add a name to the vector (notice the meat's wrapped
    ;; in a dosync
    user=> (defn add-name [name]
             (dosync
               (alter names conj name)))
    #'user/add-name
    
    user=> (add-name "zack")
    ["zack"]
    
    user=> (add-name "shelley")
    ["zack" "shelley"]
    
    ;; Notice that the var 'names' points to the reference that we created
    user=> (println names)
    #<Ref@658ba380: [zack shelley]>
    
    ;; To get the actual value of the ref, you use the '@' symbol, or deref
    user=> (println @names)
    [zack shelley]
    
    user=> (println (deref names))
    [zack shelley]
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:1857 top

(defn alter
  "Must be called in a transaction. Sets the in-transaction-value of
  ref to:

  (apply fun in-transaction-value-of-ref args)

  and returns the in-transaction-value of ref."
  {:added "1.0"}
  [^clojure.lang.Ref ref fun & args]
    (. ref (alter fun args)))
Vars in clojure.core/alter: defn ref

Comments top

No comments for alter. Log in to add a comment.