I am trying to come up with a way to convert the hash of a clojure file by applying a function from a different map to each value. Here is what I still have:
(defn transform-map [m fm]
(into {} (for [[k v] m]
(let [mfn (get fm k identity)] [k (mfn v)])))
(def m {:a 0 :b 0 :c 0 :d 0})
(def fm {:a inc :b dec :c identity})
(transform-map m fm) ;=> {:a 1, :b -1, :c 0, :d 0}
This works fine, but only as long as each function takes one argument, which is the current key value. What if I want to put a function in my function map that uses values โโdifferent from those in the same key? For example, suppose I want to put the amount of keys :aand :bin the key :d?
I can try something like:
(assoc fm :d (fn[a b] (+ a b)))
but is there a way to change my function transform-mapso that it uses the appropriate arguments in the call to that function?
stand source
share