On 9 March 2010 08:09, Meikel Brandmeyer <[email protected]> wrote:
> Hi,
>
> On Mar 8, 2:59 pm, Luka <[email protected]> wrote:
>
>> (defn leafs [m]
>>   (loop [vs (vals m), accum []]
>>     (if (empty? vs)
>>       accum
>>       (let [v (first vs)]
>>         (if (map? v)
>>           (recur (rest vs) (into accum (leafs v)))
>>           (recur (rest vs) (conj accum v)))))))
>
> How about this?
>
> (defn leafs
>  [m]
>  (reduce (fn [ls v]
>            (if (map? v)
>              (into ls (leafs v))
>              (conj ls v)))
>           [] (vals m)))

Nice, though I it's stack consuming.

There a parity between this and clojure.contrib.seq-utils/flatten
(which doesn't work with maps)...  So how about this lazy non stack
consuming alternative?

(defn leaves [m]
(filter
 (complement map?)
 (rest (tree-seq map? #(vals %)
                 m))))

user=> (leaves { :a { :b { :c "c" } :d "d" } :e "e" })
("c" "d" "e")


R.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to