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

vector

clojure.core

  • (vector)
  • (vector a)
  • (vector a b)
  • (vector a b c)
  • (vector a b c d)
  • (vector a b c d & args)
Creates a new vector containing the args.

3 Examples top

  • ;; 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
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:302 top

(defn vector
  "Creates a new vector containing the args."
  {:added "1.0"}
  ([] [])
  ([a] [a])
  ([a b] [a b])
  ([a b c] [a b c])
  ([a b c d] [a b c d])
  ([a b c d & args]
     (. clojure.lang.LazilyPersistentVector (create (cons a (cons b (cons c (cons d args))))))))
Vars in clojure.core/vector: cons defn
Used in 225 other vars (expand)
time for sync dotimes with-in-str defrecord with-out-str with-loading-context future doto proxy-super memfn when-first with-precision or condp with-local-vars with-bindings when-let amap while case deftype if-let and locking definterface defmulti proxy defonce areduce binding with-sh-dir with-sh-env with-bindings formatter-out formatter with-pprint-dispatch dir deftest- assert-predicate deftest testing assert-any with-test-out set-test with-test with-junit-output with-tap-output xml-zip deftype capture-and-send with-command-line cond-let handler-case defalias defnk <- indexed with-in-reader with-out-append-writer with-out-writer deferror throw-msg continue continue-with with-handler do-not-handle defadt with-in-reader with-out-append-writer with-out-writer with-system-properties with-connection formatter-out log spy with-logs lazy-get expect with-monad monad monad-transformer formatter with-pprint-dispatch pprint-logical-block binomial with-profile-data prof with-system-properties rec-seq rec-seq indexed with-sh-dir with-sh-env with-sh-dir with-sh-env with-query-results transaction with-connection docodepoints dochars defst docodepoints dochars do-swing do-swing-and-wait deftest- assert-predicate deftest testing assert-any with-test-out set-test with-test dotrace deftrace match with-ns with-temp-ns defservice time-series-plot box-plot add-box-plot add-categories stacked-bar-chart bar-chart heat-map xy-plot pie-chart scatter-plot dynamic-scatter-plot line-chart function-plot add-points histogram sliders add-function add-lines stacked-area-chart area-chart dynamic-xy-plot add-histogram sliders* $fn with-data with-rotation sketch with-translation lee-distance hamming-distance minkowski-distance chebyshev-distance deriv-fn deriv-fn* define-ant-task define-ant-type deftarget compile defproject repl-server form-for-testing-namespaces testing-fn for-these it defconfig expand-subcontext-forms defn! expect fail for-these collection-generator for-all context-lines testing-fn it with-timing rewrite-=> debug-repl given and when then with-re-defn transform-content snippet* defsnippets transformation clone-for let-select lockstep-transformation with-emacs-package dothread-keeping-clj continuously dothread-keeping one-of? returning keep-bindings dothread dothread-swank with-package-tracking with-timeout with-connection with-swank-io call-being-tested expect fake with-default-identity memfn-varargs with-connection with-default-session-options with-ssh-agent general-button add-action-listener add-listener label shelf general-panel jlist stack frame list-model tree add-list-selection-listener expect

Comments top

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