> I'd appreciate suggestions and insights on how I can collapse a nested
> map with "n" number of keys (levels) to create a flat map which is
> comprised of composite keys and a value. For example, let's say we
> have:
>
> { "a" { 2011 [ [ "a" 2011 "ari"] [ "a" 2011 "dan"] ] } { 2010 [ [ "a"
> 2010 "jon"] ] } }
>
> I'd like to collapse the nest into a sequence of flat maps like:
>
> ( { [a 2011] [ ["a" 2011 "ari"] ["a" 2011 "dan"] ] }, { [a 2010]
> [ ["a" 2010 "jon"] ] } )

I've put together a solution (see below) that collapses nested maps
into a sequence of flat maps with composite keys. Unfortunately, the
solution wraps the result within another list, anyone know why?
Thanks.

(defn collapse [data & keys-coll]
  (if (map? data)
    (for [[k v] data]
      (collapse v (if (nil? keys-coll)
                    (conj [] k)
                    (conj (into [] keys-coll) k))))
  (hash-map (flatten keys-coll) data)))

-- 
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