I've been trying to implement stacks using vectors in Clojure. In case
you wonder why, it's because I need stacks with a fast item count
operation. While experimenting I discovered some vector properties which
were unexpected.

conj adds at the end of the vector as expected:

user> (conj [1 2 3] 4)
[1 2 3 4]

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)

I don't know if it also has performance implications (does it turn my
vector into a list?). 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> 

I didn't expect this to happen. Most tutorials (and Stuart Halloway's
book) emphasize the generality of the sequence functions, only
mentioning that subvec is faster for vectors. I think the fact that they
also change subsequent behavior of conj is fundamental and should be
printed in bold type.

Am I missing something here?

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)

--J.

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to