vector

added
1.0

ns
clojure.core

type
function

(vector) (vector a) (vector a b) (vector a b c) (vector a b c d) (vector a b c d e) (vector a b c d e f) (vector a b c d e f & args)

Creates a new vector containing the args.

                ;; create an empty vector the long way
user=> (vector)
[]

;; create an empty vector the short way
user=> []
[]

;; you can even create vectors with nil values
user=> (vector nil)
[nil]

;; create a vector the long way
user=> (vector 1 2 3)
[1 2 3]

;; create a vector the short way
user=> [1 2 3]
[1 2 3]

;; checking for the 2 results above
user=> (class (vector 1 2 3))
clojure.lang.PersistentVector

user=> (class [1 2 3])
clojure.lang.PersistentVector

user=> (= (vector 1 2 3) [1 2 3])
true


            
                ;; Destructuring with a vector, inside a "let" form, a simple case (a symbol
;; for each element):

;; destructuring with an inline vector
user=> (let [[first-element second-element third-element fourth-element] 
             [10 20 30 40]] 
         (str "first=" first-element " second=" second-element 
           " third=" third-element " fourth=" fourth-element))
"first=10 second=20 third=30 fourth=40"
;; notice how 4 symbols were created pointing to the scalars 10, 20, 30 and 40


;; destructuring with a symbol to a vector
user=> (def my-vector [1 2 3 4])
#'user/my-vector

user=> (let [[first-element second-element third-element fourth-element] my-vector] 
         (str "first=" first-element " second=" second-element 
           " third=" third-element " fourth=" fourth-element))
"first=1 second=2 third=3 fourth=4"
            
                ;; Destructuring with a vector, inside a "let" form, more complex cases:

user=> (let [[first-element second-element & the-rest] my-vector] 
         (str "first=" first-element " second=" second-element " 
           the-rest=" the-rest))
"first=1 second=2 the-rest=(3 4)"
;; notice how "the-rest" is a sequence

user=> (let [[first-element second-element third-element fourth-element 
               :as everything] 
             my-vector] 
         (str "first=" first-element " second=" second-element " 
           third=" third-element " fourth=" fourth-element " 
           everything=" everything))
"first=1 second=2 third=3 fourth=4 everything=[1 2 3 4]"
;; notice how "everything" is the whole vector
            
                ;; Create pairs from a seq
;; (1 2 3 4) -> ([1 2] [2 3] [3 4])


(def inp (list 1 2 3 4))
;; (1 2 3 4)
(map vector inp (drop 1 inp))
;; ([1 2] [2 3] [3 4])