Best way to loop a map of maps

1,190 views
Skip to first unread message

Ryan

unread,
Dec 3, 2013, 6:05:14 PM12/3/13
to clo...@googlegroups.com
Hi all,

I am trying to figure out a better way to loop the following map than using nested doseq. The map has the following structure:

(def m
  {"outerKeyA" {:innerKeyA {"string id" {:foo 1 :bar 2}}}
   "outerKeyB" {:innerKeyB {"string id" {:bar 5 :baz 10}}}})

So, right now i am doing the following:

(doseq [[outer-keys collections] m]
  (doseq [[collection-name collection] collections]
    (doseq [[string-id data] collection]
      ;; do stuff with all the above 
)))

Is there a more idiomatic/better way to do deeply nested iterations/traversal of map of maps?

Thank you for any replies!

Ryan 

Jay Fields

unread,
Dec 3, 2013, 6:08:36 PM12/3/13
to clo...@googlegroups.com
I was going to type in the example with multiple bindings, but this
will probably be more helpful:
http://blog.jayfields.com/2013/05/clojure-combining-calls-to-doseq-and-let.html
> --
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@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 unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Cedric Greevey

unread,
Dec 3, 2013, 6:26:02 PM12/3/13
to clo...@googlegroups.com
Tree-seq? But then, if the data is structured so each level has a distinct purpose, that's not really a great fit.

Perhaps we need a for/doseq analog of assoc-in, update-in, etc.?

At the very least I think this might work:

(doseq [[outer-keys collections] m
        [collection-name collection] collections
        [string-id data] collection]
  ;; do stuff with the above
)

James Ferguson

unread,
Dec 3, 2013, 6:27:57 PM12/3/13
to clo...@googlegroups.com
`update-in` could be helpful, depending on what exactly you're doing.

(doseq [keyA keys, keyB otherkeys]
  (update-in m [keyA keyB] some-function))

Ryan

unread,
Dec 5, 2013, 5:06:41 AM12/5/13
to clo...@googlegroups.com
Thanks guys for the useful answers :)

Ryan

Jeroen van Dijk

unread,
Dec 10, 2013, 3:29:51 AM12/10/13
to clo...@googlegroups.com
Don't forget about the option of walking over a map by using clojure.walk/postwalk or clojure.walk/prewalk

I use these functions often lately. Especially useful if you have to update values in your map and they could occur anywhere. It is a bit tricky to get working at first, because the return value of each step needs to match the structure that is currently walked. For that reason you sometimes need to switch between a postwalk instead of a prewalk etc.

Try this for a demo

(def m
  {"outerKeyA" {:innerKeyA {"string id" {:foo 1 :bar 2}}}
   "outerKeyB" {:innerKeyB {"string id" {:bar 5 :baz 10}}}})

(clojure.walk/postwalk-demo m)




Ryan

unread,
Dec 10, 2013, 4:15:12 AM12/10/13
to clo...@googlegroups.com
I've seen postwalk and prewalk but never really played around with them so I will give it a shot.

Thanks for your input!

Ryan

Thomas Heller

unread,
Dec 10, 2013, 6:48:56 AM12/10/13
to clo...@googlegroups.com
FWIW you can simplify the nested doseqs like this

(doseq [[outer-keys collections] m
            [collection-name collection] collections
            [string-id data] collection]
  ;; do stuff with the above
  )

HTH,
/thomas

Thomas Heller

unread,
Dec 10, 2013, 6:57:38 AM12/10/13
to clo...@googlegroups.com
Oh nvm, just saw that it was suggested before.

But maybe this is new:

(def m
  {"outerKeyA" {:innerKeyA {"string id" {:foo 1 :bar 2}}}
   "outerKeyB" {:innerKeyB {"string id" {:bar 5 :baz 10}}}})

(defn nested-seq [m]
  (for [[outer-key collections] m
        [collection-name collection] collections
        [string-id data] collection]
    {:outer-key outer-key :collection-name collection-name :string-id string-id :data data}
    ))

(pprint (nested-seq m))

;; produces
({:outer "outerKeyA",
  :collection-name :innerKeyA,
  :string-id "string id",
  :data {:foo 1, :bar 2}}
 {:outer "outerKeyB",
  :collection-name :innerKeyB,
  :string-id "string id",
  :data {:bar 5, :baz 10}})



for list comprehensions behave like doseq except that they return whatever the body does, thus letting you move your side effects somewhere else.

Maybe this helps,
/thomas

Ryan

unread,
Jan 21, 2014, 8:00:57 AM1/21/14
to clo...@googlegroups.com
Thank you for your input Thomas.

All replies have definitely been very useful :)

Cheers,

Ryan
Reply all
Reply to author
Forward
0 new messages