2

Suppose I have a map:

{:name "foo"
 :age "bar"}

And another one

{:name (fn [val] (println val))
 :age (fn [val] (= val "bar"))}

I want to apply function keyed by :name on second map to the first map, which also keyed by :name and the function keyed by :age to the first map which keyed by :age. How to do this the clojure way?

Budi Sutrisno
  • 599
  • 1
  • 5
  • 12

4 Answers4

8

You can use merge-with

(def m1 {:name "foo"
         :age "bar"})

(def m2 {:name (fn [val] (println val))
         :age (fn [val] (= val "bar"))})

user=> (merge-with #(%1 %2) m2 m1)
foo
{:name nil, :age true}
edbond
  • 3,921
  • 19
  • 26
  • 1
    This is concise, but only works if the two maps have exactly the same keys, which isn't really clear from the question. – amalloy Mar 12 '14 at 18:47
  • @amalloy Thanks for clarification, I found it's a great usage example for a *rare* used function. – edbond Mar 12 '14 at 21:37
1

map over one map and get corresponding function from the other one.

(def m1 {:name "foo"
         :age "bar"})

(def m2 {:name (fn [val] (println val))
         :age (fn [val] (= val "bar"))})

(map (fn [[k v]]
       ((get m2 k) v)) 
     m1)

Each iteration over the map passes a vector to the function, in your sample:

[:name "foo"]
[:age "bar"]

So destructuring the function parameter into [[k v]] gives you each key/value separately.

guilespi
  • 4,672
  • 1
  • 15
  • 24
1
(def data { :name "don knotts" 
        :dob "1/1/1940" 
        :cob "Valdosta"  })

(def fxns {:name identity :dob identity :cob clojure.string/reverse})

(defn bmap [data fxn]
  (apply merge (for [[k1 d] data [k2 f] fxn  :when (= k1 k2)]
     {k1 (f d)})))

;=user>{:cob "atsodlaV", :dob "1/1/1940", :name "don knotts"}
KobbyPemson
  • 2,519
  • 1
  • 18
  • 33
0

I like this, if you need more resilience:

(defn fmm [m fm]
  (let [f (fn [k] ((get fm k identity) (k m)))
        ks (keys m)]
    (zipmap ks (map f ks))))
Alister Lee
  • 2,236
  • 18
  • 21
  • Also, check out the answers to this [question](http://stackoverflow.com/questions/1676891/mapping-a-function-on-the-values-of-a-map-in-clojure?rq=1) – Alister Lee Mar 14 '14 at 03:32