You're viewing version 1.2.0 of vec. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16
  • (vec coll)
Creates a new vector containing the contents of coll.

2 Examples top

  • user=> (vec '(1 2 3))
    [1 2 3]
    
    user=> (vec [1 2 3])
    [1 2 3]
    
    user=> (vec '())
    []
    
    user=> (vec nil)
    []
  • ;; Warning.  If the arg is a Java array, then the returned vector will alias it,
    ;; and modifying the array will thus modify the vector.  To avoid this, do
    ;; not modify the array after the vec call.  One way to guarantee this is to
    ;; make a copy of the array, call vec on the new array, and then lose all
    ;; references to the copy so it cannot be accessed in any way.
    
    user=> (def a (to-array (repeat 4 0)))
    #'user/a
    user=> (seq a)
    (0 0 0 0)
    user=> (def v (vec a))
    #'user/v
    user=> v
    [0 0 0 0]
    
    ;; Now change a, and v changes, too, since they share state.
    user=> (aset a 2 -5)
    -5
    user=> v
    [0 0 -5 0]
    
    ;; One way to avoid this
    user=> (def v (vec (aclone a)))
    #'user/v
    user=> v
    [0 0 -5 0]
    user=> (aset a 2 -20)
    -20
    user=> v
    [0 0 -5 0]
    
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:313 top

(defn vec
  "Creates a new vector containing the contents of coll."
  {:added "1.0"}
  ([coll]
   (if (instance? java.util.Collection coll)
     (clojure.lang.LazilyPersistentVector/create coll)
     (. clojure.lang.LazilyPersistentVector (createOwning (to-array coll))))))
Vars in clojure.core/vec: defn instance? to-array

Comments top

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