On Mon, Jul 13, 2009 at 11:15 AM, Jan Rychter<j...@rychter.com> wrote: > > However, after reading "Programming Clojure" (page 114) I expected to be > able to use sequence functions, such as drop-last, without my vectors > changing representation. It seems this is not the case, as after calling > drop-last (or indeed any of the seq functions) conj no longer adds at > the end: > > user> (conj (drop-last 1 [1 2 3]) 4) > (4 1 2)
That's correct -- seq functions return seqs, not vectors. > I don't know if it also has performance implications (does it turn my > vector into a list?). drop-last returns a lazy seq, which is why conj'ing onto it adds items to the left. Also, lazy-seqs don't know their size -- counting is a linear time operation. user=> (class (drop-last 1 [1 2 3])) clojure.lang.LazySeq user=> (counted? (drop-last 1 [1 2 3])) false > So it turns out that pretty much the only > operations I can use on my vectors are subvec and pop: > > user> (conj (subvec [1 2 3] 0 2) 4) > [1 2 4] > user> (conj (pop [1 2 3]) 4) > [1 2 4] > user> As listed here, you can also use 'assoc' and 'replace': http://clojure.org/data_structures#Vectors > And while we're on the subject -- any hints for implementing a stack > with a fast item count? (apart from a list with a separate counter) Well, you already found 'conj' and 'pop' on vector, and the link above also lists 'peek' -- is that insufficient? Vectors can return their size in constant time: user=> (peek [1 2 3]) 3 user=> (counted? [1 2 3]) true If you don't want the partial-copying that vectors do on conj an pop, Clojure's normal list type PersistentList also knows its size and supports 'conj', 'pop', and 'peek': user=> (class '(3 2 1)) clojure.lang.PersistentList user=> (conj '(3 2 1) 4) (4 3 2 1) user=> (peek '(3 2 1)) 3 user=> (pop '(3 2 1)) (2 1) --Chouser --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---