Created
May 5, 2022 09:58
-
-
Save fjsousa/7e9b702c30dcea86ce1a1b6553072d8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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