Rom1
Rom1

Reputation: 3207

idiomatic map join in clojure?

I hava a data structure that looks like this:

(def conf 
  { :devices [{:alias "OSC Sender",
               :name "OSC Sender",
               :ins [{:name "xpos", :type :int, :mutable true}]},
              {:alias "const2", :name "const",
               :outs [{:name "out", :type :int}]}],
    :connections {"const2.out" "OSC Sender.xpos"},
    :layout [{:alias "const2",
              :x 72.12447405329594,
              :y 99.88499298737729},
             {:alias "tick",
              :x 82.5732819074334,
              :y 133.91374474053296},
             {:alias "OSC Sender",
              :x 185.17741935483872,
              :y 113.90322580645162}]})

I would like to join maps in :devices and :layout by key (specifically :alias) to enrich the devices with layout information.

Right now I cobbled the following solution:

(map (partial reduce merge) (vals (group-by :alias (concat (:devices conf) (:layout conf)))))

Is that an idiomatic join or is something else preferable?

Cheers

Upvotes: 2

Views: 730

Answers (1)

Michał Marczyk
Michał Marczyk

Reputation: 84341

You can use the join function from the clojure.set namespace:

(clojure.set/join (conf :devices) (conf :layout) {:alias :alias})

Note that the return value is a set. Omitting the final argument results in a natural join; see (doc clojure.set/join) for details.

Upvotes: 7

Related Questions