Re: Get sequence of values in arbitrarily nested collection

2010-12-06 Thread Benny Tsai
Nice! I knew there had to be a nicer way of traversing nested collections :) Thank you for this. On Dec 6, 8:30 am, Justin Kramer wrote: > tree-seq makes this pretty simple: > > (defn nested-vals [key coll] >   (for [x (tree-seq coll? seq coll) :when (contains? x key)] >     (get x key))) > > T

Re: Get sequence of values in arbitrarily nested collection

2010-12-06 Thread Alyssa Kwan
+1 Lazy is better. Personally, I would have used filter and map instead of for, but this is probably clearer. Thanks, Alyssa On Dec 6, 10:30 am, Justin Kramer wrote: > tree-seq makes this pretty simple: > > (defn nested-vals [key coll] >   (for [x (tree-seq coll? seq coll) :when (contains? x k

Re: Get sequence of values in arbitrarily nested collection

2010-12-06 Thread Justin Kramer
tree-seq makes this pretty simple: (defn nested-vals [key coll] (for [x (tree-seq coll? seq coll) :when (contains? x key)] (get x key))) This works with any type of key and all associative Clojure structures. It could be made compatible with Java structures by swapping out the 'coll?' predi

Re: Get sequence of values in arbitrarily nested collection

2010-12-06 Thread Ken Wesson
On Mon, Dec 6, 2010 at 2:57 AM, Benny Tsai wrote: > When I saw the part about traversing an arbitrarily nested collection, > I immediately thought of clojure.walk (http://clojure.github.com/ > clojure/clojure.walk-api.html).  I ended up with this: > > (use 'clojure.walk) > > (defn all-vals [k coll

Re: Get sequence of values in arbitrarily nested collection

2010-12-05 Thread Benny Tsai
When I saw the part about traversing an arbitrarily nested collection, I immediately thought of clojure.walk (http://clojure.github.com/ clojure/clojure.walk-api.html). I ended up with this: (use 'clojure.walk) (defn all-vals [k coll] (let [vals (atom []) find-val (fn [form]

Re: Get sequence of values in arbitrarily nested collection

2010-12-05 Thread Ken Wesson
On Sun, Dec 5, 2010 at 10:03 PM, Ken Wesson wrote: > On Sun, Dec 5, 2010 at 9:12 PM, Alex Baranosky > wrote: >> Hi guys, >> I would like a function to be able to take an arbitrarily nested collection >> and return a sequence of all values of a given key, such as :name, that >> appears anywhere in

Re: Get sequence of values in arbitrarily nested collection

2010-12-05 Thread Ken Wesson
On Sun, Dec 5, 2010 at 9:12 PM, Alex Baranosky wrote: > Hi guys, > I would like a function to be able to take an arbitrarily nested collection > and return a sequence of all values of a given key, such as :name, that > appears anywhere in the nested collection. > Does anything like this already ex

Re: Get sequence of values in arbitrarily nested collection

2010-12-05 Thread Alex Baranosky
The above is using Midje syntax: https://github.com/marick/Midje -- 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

Re: Get sequence of values in arbitrarily nested collection

2010-12-05 Thread Alex Baranosky
Here's my first attempt: (defn all-vals [key coll] (let [all-vals-w-key (partial all-vals key)] (cond (map? coll) (concat [(key coll)] (all-vals-w-key (vals coll))) (or (vector? coll) (seq? coll)) (apply concat (map all-vals-w-key coll) Call it like so: (fact

Get sequence of values in arbitrarily nested collection

2010-12-05 Thread Alex Baranosky
Hi guys, I would like a function to be able to take an arbitrarily nested collection and return a sequence of all values of a given key, such as :name, that appears anywhere in the nested collection. Does anything like this already exist? Thanks for the help, Alex -- You received this message