Clojure: How do I apply a function to a subset of the entries in a hash-map?

后端 未结 3 1266
鱼传尺愫
鱼传尺愫 2021-02-06 12:43

I am not to Clojure and attempting to figure out how to do this.

I want to create a new hash-map that for a subset of the keys in the hash-map applies a function to the

3条回答
  •  北海茫月
    2021-02-06 13:07

    (defn doto-map [m ks f & args]
      (reduce #(apply update-in %1 [%2] f args) m ks))
    

    Example call

    user=> (doto-map {:a 1 :b 2 :c 3} [:a :c] + 2)
    {:a 3, :b 2, :c 5}
    

    Hopes this helps.

提交回复
热议问题