Skip to content

clojure.core/subvec

View this page on ClojureDocs

Type: function Added: Clojure 1.0 Examples: 4 Runnable: 2
([v start] [v start end])

Returns a persistent vector of the items in vector from start (inclusive) to end (exclusive). If end is not supplied, defaults to (count vector). This operation is O(1) and very fast, as the resulting vector shares structure with the original and no trimming is done.

Examples

by zmila on
by freckletonj on
by Sophia-Gold on . May have evaluation errors.
by jonathan-chen-tda on . May have evaluation errors.

Note by reborg

;; Note that subvec holds a reference to the original vector. If you plan 
;; to split a large vector and never using it again, consider freeing up
;; the reference.

(let [v1 (subvec (vec (range 1e7)) 0 5)
      v2 (subvec (vec (range 1e7)) 5 10)]
      (into v1 v2))
;; OutOfMem

(let [v1 (into [] (subvec (vec (range 1e7)) 0 5))
      v2 (into [] (subvec (vec (range 1e7)) 5 10))]
      (into v1 v2))
;; [0 1 2 3 4 5 6 7 8 9]

See also


Content from the matching ClojureDocs page, with authors credited on each contribution.