Thank you Laurent and Ken. Both great responses. What I was forgetting was that vector is a function of the index.
This now makes total sense and I feel like a dummy for not getting it. Thanks for your responses. On Feb 3, 3:44 pm, Laurent PETIT <laurent.pe...@gmail.com> wrote: > 2011/2/3 Base <basselh...@gmail.com>: > > > > > > > Thats the one! > > > Now I just have to understand it... > > > The first apply is throwing me a little. > > > On Feb 3, 3:29 pm, Laurent PETIT <laurent.pe...@gmail.com> wrote: > >> Hello, > > >> 2011/2/3 Base <basselh...@gmail.com>: > > >> > Hi All - > > >> > I recall seeing a beautiful method for doing the following on this > >> > site, but cannot seem to find it.... > >> > I would like to transform > > >> > (def a [[1 2 3] > >> > [4 5 6] > >> > [7 8 9]]) > > >> > to > > >> > [[1 4 7] > >> > [2 5 8] > >> > [3 6 9]] > > >> user=> (vec (apply map vector a)) > >> [[1 4 7] [2 5 8] [3 6 9]] > > Let's get rid of the call to vec, and focus on the core: > (apply map vector a) : how do we get to this ? > > let's start inside out: we want to take advantage of the fact that the > function map can take several colls. This way, it will first apply its > function to all the first items of the colls, then the second items, > etc. That's it! > > user=>(map (fn [c1 c2 c3] [c1 c2 c3]) [:c1.1 :c1.2 :c1.3] [:c2.1 :c2.2 > :c2.3] [:c3.1 :c3.2 :c3.3]) > ([:c1.1 :c2.1 :c3.1] [:c1.2 :c2.2 :c3.2] [:c1.3 :c2.3 :c3.3]) > > Then we realize that (fn [c1 ...] [...]) is just a special case of the > function vector, so : > user=>(map vector [:c1.1 :c1.2 :c1.3] [:c2.1 :c2.2 :c2.3] [:c3.1 :c3.2 :c3.3]) > ([:c1.1 :c2.1 :c3.1] [:c1.2 :c2.2 :c3.2] [:c1.3 :c2.3 :c3.3]) > > And then, hmm, we would like to generalize the above so that it works > with any number of collections. The function vector already allows > this. And Clabamgo ! The apply function is really cool, because it > accepts that its last argument be a seq, so we can rewrite the above: > > user=>(apply map vector (list [:c1.1 :c1.2 :c1.3] [:c2.1 :c2.2 :c2.3] > [:c3.1 :c3.2 :c3.3])) > ([:c1.1 :c2.1 :c3.1] [:c1.2 :c2.2 :c3.2] [:c1.3 :c2.3 :c3.3]) > > That's it, the list in the last position of the call to apply can now > be of any size. > > HTH, > > -- > Laurent > > > > > > >> Cheers, > > >> -- > >> Laurent > > > -- > > 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 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