title: Fixed length stack structure in Clojure
body: <p>What's the best data structure for a fixed length stack (I originally called it a queue, but what I want is a stack) where items are added to the front, and every time an item is added to the front an item is removed from the end? Various lengths of subvectors will be accessed from the front also. I was using vectors, now thinking about clojure.lang.PersistentQueue and finger trees.</p>
  
  <p>edit, to clarify, something like:</p>
  
  <pre><code>&gt; (def q (queue [1 2 3 4]))
  [1 2 3 4]
  &gt; (push q 9 8 7)
  [7 8 9 1]
  &gt; (peek (push q 9 8 7))
  7
  </code></pre>
  
  <p>edit2: thanks for all your answers so far, this has turned into an exercise in going back to basics and reading Joy of Clojure, learning for instance that subvec of subvec retains a reference to the first subvec's vector, while something like (vec (cons x (subvec... would if used repeatedly accrue references to all intermediate subvecs. In light of this, how about this implementation of push for a vector-based queue ?:</p>
  
  <pre><code>(defn push [v i n] (if (&gt;= (count v) n) (conj (subvec v 1 n) i) (conj v i) ) )
  </code></pre>
  
  <p>then the resulting vector could be accessed via rseq which I believe is fast with vectors (due to its use of index-offset?)</p>