You should use a vector, but it's also possible to use concat. For example, 
(concat '(1 2 3) [4]) will give you (1 2 3 4).

This made me curious as to the best way to get a collection into vector, so 
I played around with it some:

user=> (def r 100000)
> #'user/r
> user=> (def coll (range 10000))
> #'user/coll
> user=> (def coll-v (into [] coll))
> #'user/coll-v
> user=> (time (dotimes [_ r] (conj (into [] coll) :a)))
> "Elapsed time: 14074.018464 msecs"
> nil
> user=> (time (dotimes [_ r] (conj (apply vector coll) :a)))
> "Elapsed time: 22565.594515 msecs"
> nil
> user=> (time (dotimes [_ r] (conj (vec coll) :a)))
> "Elapsed time: 22424.174719 msecs"
> nil
> user=> (time (dotimes [_ r] (concat coll '(:a))))
> "Elapsed time: 5.366059 msecs"
> nil
> user=> (time (dotimes [_ r] (concat coll-v '(:a))))
> "Elapsed time: 5.56465 msecs"
> nil
> user=> (time (dotimes [_ r] (conj coll-v :a)))
> "Elapsed time: 10.65771 msecs"
> nil
> user=> (time (dotimes [_ r] (concat coll coll)))
> "Elapsed time: 6.048041 msecs"
> nil
> user=> (time (dotimes [_ r] (apply conj coll-v coll-v)))
> "Elapsed time: 72414.847105 msecs"
> nil
>

Surprisingly it looks like (concat coll '(:a)) is faster than (conj coll-v 
:a). That's not really what I would expect; does anybody have a good 
explanation for this? Did I just bork the test somehow, or - I mean, 
obviously concat's pretty fast but I was expecting conj to be on the level. 
In fact, if you convert and then conj it's significantly slower than using 
concat.

...not that it'd really matter, in basically all cases, since (into [] ...) 
is definitely still in the "fast enough" category. Still, if you're 
building a sequence, what's the reasoning against using (concat coll ...) 
instead of (conj (into [] ...) ...)? Is it a matter of elegance, or is 
there a specific practical reason?

On Friday, February 7, 2014 8:06:20 PM UTC-8, Armando Blancas wrote:
>
> For efficient appends at the end you need a vector. Using the sequence 
> library can be tricky while you're putting together your data structures 
> because it's likely that you'll not done yet with type-specific functions. 
> You'll need to re-create your vector after using map/filter/etc to be able 
> to keep adding at the end. 
>
> On Friday, February 7, 2014 4:20:09 PM UTC-8, t x wrote:
>>
>> Consider the following: 
>>
>> (cons 1 '(2 3 4)) ==> (1 2 3 4) 
>> (cons 1 [2 3 4])  ==> (1 2 3 4) 
>>
>> (conj '(a b c) 1) ==> (1 a b c) 
>> (conj '[a b c] 1) ==> [a b c 1] 
>>
>>
>> ================================ 
>>
>> Now, I would like something that _always_ 
>>   * appends to the end 
>>
>> cons is almost what I want, except it always appends to front. 
>>
>> conj is not what I want -- in fact, I'm afraid of conj. Often times, 
>> I'll run map/filter on something, and suddenly, instead of a vector, I 
>> now have a list -- and conj changes the order of the item added. 
>>
>> Thus, my question: is there a builtin to _unconditinoally_ append to 
>> the end of a list/sequence/vector? 
>>
>> Thanks! 
>>
>

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to