Merge two lists of maps, combining the maps together on a specific key

Viewed 1372

I'm running two select statements against Cassandra, so instead of having a join I need to join them in code. Being relatively new to Clojure, I'm having a hard time doing this without resorting to really ugly nested loops. Furthermore, if table-b is missing a matching entry from table-a, it should add default table-b values.

The two selects each result in a list of maps (each "row" is one map). The id key is a UUID, not string.

Here's how the selects look if I def something with the same structure.

(def table-a (list {:id "105421db-eca4-4500-9a2c-08f1e09a35ca" :col-b "b-one"}
                   {:id "768af3f3-3981-4e3f-a93d-9758cd53a056" :col-b "b-two"}))

(def table-b (list {:id "105421db-eca4-4500-9a2c-08f1e09a35ca" :col-c "c-one"}))

I want the end result to be this:

({:id "105421db-eca4-4500-9a2c-08f1e09a35ca" :col-b "b-one" :col-c "c-one"}
 {:id "768af3f3-3981-4e3f-a93d-9758cd53a056" :col-b "b-two" :col-c "default-value"})

Thanks for any help.

1 Answers
Related