ClojureDocs

Nav

Namespaces

vec

clojure.core

Available since 1.0 (source)
  • (vec coll)
Creates a new vector containing the contents of coll. Java arrays
will be aliased and should not be modified.
2 Examples
user=> (vec '(1 2 3))
[1 2 3]

user=> (vec [1 2 3])
[1 2 3]

user=> (vec #{1 2 3})
[1 2 3]

user=> (vec {:a 1 :b 2 :c 3})
[[:a 1] [:b 2] [:c 3]]

user=> (vector {:a 1 :b 2 :c 3})
[{:a 1, :b 2, :c 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]
See Also

Creates a new vector containing the args.

Added by alimoeeny

Return true if x implements IPersistentVector

Added by alimoeeny

Creates a new vector of a single primitive type t, where t is one of :int :long :float :double :by...

Added by alimoeeny
0 Notes
No notes for vec