puzzler wrote:
> subvec is O(1) because it takes advantage of sharing. This is quite
> useful.
>
> Is there a way to write concatvec in an O(1) way, taking advantage of
> sharing?
> I suspect that the "obvious way" to concatenate vectors, i.e., (into
> [] (concat v1 v2)), would be O(n).
Another way would be to create a proxy of the vector class, eg:
(defn concat-vector [a b]
(proxy [clojure.lang.APersistentVector] []
(seq [] (concat a b))
(length [] (+ (count a) (count b)))
(cons [e] (conj (reduce conj a b) e))
(nth [i] (if (< i (count a)) (a i) (b (- i (count a)))))))
which threads the operations to the underlying vectors. Given this
simple clojure implementation, its only faster for big (~ more than
100 elements) vectors, and if you conj onto it, its doing the work of
concat anyway.
--~--~---------~--~----~------------~-------~--~----~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---