Hi, I actually wrote a sort of counterpart to leafs a while back:

(defn- rmap
  "Implementation core for map-at-levels."
  [func obj lvls lvl]
  (let [children-mapped (if (coll? obj)
                          (for [c obj]
                            (rmap func c lvls (inc lvl)))
                          obj)]
    (if (.contains lvls lvl)
      (func children-mapped)
      children-mapped)))

(defn map-at-levels
  "Applies map to a recursive data structure up to the specified level. The
   collection itself is level 0, and its members are at level 1, etc."
  [func coll lvls]
  (rmap func coll lvls 0))

I had wanted to get a part of the functionality that Mathematica's Map
function provides, letting you map a function only at certain levels
of structure nesting. It's two functions because I didn't  want the
user to be able to call it with their own starting level, and didn't
know how else to do that. But also, it seemed to be working and then
shortly after that I realized I didn't need it and never worked with
it further, so it might be improved. I had intended to go on to write
a function like leafs after that (one that would map the function only
into data structure elements that weren't themselves collections). I'd
love to see some contrib that collected some more functions for
dealing with nested data structures.

  - David

On Mon, Mar 8, 2010 at 5:59 AM, Luka <luka.stojano...@gmail.com> wrote:
>
> I'm aware of clojure-dev group, but being new to clojure, i thought I
> should start here first.
> These are two methods I've needed in my project and I didn't found
> better way to accomplish these tasks. First function returns vector of
> leaf nodes in a map, and second dissociates in nested map. First
> question would be is there already a way in clojure to do this? If
> not, does these kind of functions deserve place in contrib, and third,
> what should I do to make this functions production ready, as in if
> they were really part of contrib?
>
> (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)))))))
>
> (defn dissoc-in [m v]
>  (let [len (count v)
>        cur (get-in m v)]
>    (cond
>      (nil? cur) m
>      (= len 0) m
>      (= len 1) (dissoc m (first v))
>      true (update-in m (butlast v) #(dissoc % (last v))))))
>
> Other thing I would like to ask is how can I see what is different in
> 40 github clones of clojure.contrib without clicking on every clone?
>
> Cheers,
> Luka
>
> --
> 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

Reply via email to