How to Iterate over Map Keys and Values in Clojure?

后端 未结 4 1850
感动是毒
感动是毒 2020-12-23 15:48

I have the following map which I want to iterate:

(def db {:classname \"com.mysql.jdbc.Driver\" 
         :subprotocol \"mysql\" 
         :subname \"//100.         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-23 16:38

    It's not totally clear if you are trying to solve something beyond just printing out values (side effects), and if that's all you're going for then I think the doseq solution above would be the most idiomatic. If you want to do some operations on the keys and values of the map and return the same type of data structure, then you should have a look at reduce-kv, for which you can find the docs for here

    Like reduce, reduce-kv accepts a function, a starting value/accumulator, and some data, but in this case the data is a map instead of a sequence. The function gets passed three args: the accumulator, current key, and current value. If you do want to do some data transformation and return some data, this would seem like the right tool for the job to me.

提交回复
热议问题