Is is possible to destructure a clojure vector into last two items and the rest?

user=> (def v (vec (range 0 10000000)))
#'user/v
user=> (time ((fn [my-vector] (let [[a b] (take-last 2 my-vector)] (+ a b))) v))
"Elapsed time: 482.965121 msecs"
19999997
user=> (time ((fn [my-vector] (let [a (peek my-vector) b (peek (pop my-vector))] (+ a b))) v))
"Elapsed time: 0.175539 msecs"
19999997

My advice would be to throw convenience to the wind and use peek and pop to work with the end of a vector. When your input vector is very large, you'll see tremendous performance gains.

(Also, to answer the question in the title: no.)


You can peel off the last two elements and add them thus:

((fn [v] (let [[b a] (rseq v)] (+ a b))) [1 2 3 4])
; 7
  • rseq supplies a reverse sequence for a vector in quick time.
  • We just destructure its first two elements.
  • We needn't mention the rest of it, which we don't do anything with.