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
+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
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
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
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]
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
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
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
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
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
10 matches
Mail list logo