On Tue, Feb 7, 2012 at 3:28 PM, Manuel Paccagnella <manuel.paccagne...@gmail.com> wrote: > I've some questions about destructuring a seq of maps, like: > > 1. Is it possible? > 2. If yes, does it have any sense? > 3. And if it's possible and is reasonable, how can I do it? > > For example, let's say that I have a list of maps like this: > > (def something [{:a 1 :b 2} > {:a 2 :b 5} > {:a 3 :b 7}]) > > And let's say that I want a function that returns the sum of all :a values > of the something seq. I could write the function in this way: > > (defn do-sum [some-coll] > (reduce + (map #(:a %) some-coll))) > > Not a big deal, but I wonder if there is a better way to do it. > Destructuring could be one answer I thought, since I need only the value > corresponding to the :a key of every map in the seq. But I didn't figured > out how to do it yet. > > Any ideas?
Well, destructuring requires a literal, so you have to know exactly the structure you're wanting to pick apart. For your specific example, I can write a destructuring usage that will work, but there's no way to do it in a general way. user=>(defn do-sum [[{a1 :a} {a2 :a} {a3 :a}]] (+ a1 a2 a3)) #'user/do-sum user=>(do-sum something) 6 -- 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