Skip to content

Instantly share code, notes, and snippets.

@fjsousa
Created May 5, 2022 09:58
Show Gist options
  • Save fjsousa/7e9b702c30dcea86ce1a1b6553072d8b to your computer and use it in GitHub Desktop.
Save fjsousa/7e9b702c30dcea86ce1a1b6553072d8b to your computer and use it in GitHub Desktop.
(defn full-outer-join
"merge two collections on key. Equivalent to SQL full outer join.
assumes keys are unique"
[k A B]
(let [A' (->> A
(group-by k)
(map (fn [[k v]] [k (first v)]))
(into {}))
B' (->> B
(group-by k)
(map (fn [[k v]] [k (first v)]))
(into {}))]
(map (fn [[k v]] v)
(merge-with merge A' B'))))
(comment
(def A [{:k 1 :foo "bar"}
{:k 2 :foo "bla"}])
(def B [{:k 2 :wtv 123}
{:k 3 :wtv 456}])
(full-outer-join :k A B)
({:k 1, :foo "bar"}
{:k 2, :foo "bla", :wtv 123}
{:k 3, :wtv 456}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment