Vectors are mostly for speed and maintaining value sequence order.

Try this => (vec (reverse [1 2 3]))
Not this => (apply vector (reverse [1 2 3]))

As an alternative you can do this => (into [] (reverse [1 2 3]))

You can always use mapv when calling fun for each coll object.

Try this => (mapv #(* 2 %) [1 2 3])
Not this => (apply vector (map (partial * 2) [1 2 3]))
 
 
01.12.2013, 06:15, "Andy Smith" <the4thamig...@googlemail.com>:
Hi,

I am trying to understand the manipulation of vectors from an efficiency point of view. For example if I want to reverse a vector I can do the following

(apply vector (reverse [1 2 3]))

My understanding is that reverse will create a new list object (3 2 1) then this will be used to construct a new vector object [3 2 1]. What can I use to construct a new vector directly instead of having the intermediate list object being constructed?

I have read about rseq as providing something like a reverse iterator to the same underlying vector, which may be a great solution in this case, but my question is really about the more general case of any function that manipulates a vector e.g. the following also returns a list rather than a vector as desired,

(map (partial * 2) [1 2 3])

again forcing me to use apply vector e.g.

(apply vector (map (partial * 2) [1 2 3]))

Why dont we have a version of map that returns a vector when given a vector? We can do this kind of thing in other languages with templates/generics why not clojure?

There is obviously some basic principle/understanding that I am missing here. This kind of thing surely cant be very efficient, can it?

Andy

 

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

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