Last active
April 12, 2022 20:15
-
-
Save ataggart/d00cfde376476c9e495882a60deba904 to your computer and use it in GitHub Desktop.
left-join analog of clojure.set/join
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 left-join | |
"When passed 2 rels, returns the rel corresponding to the natural | |
left-join. When passed an additional keymap, joins on the corresponding | |
keys." | |
([xrel yrel] | |
(if (and (seq xrel) (seq yrel)) | |
(let [ks (intersection (set (keys (first xrel))) | |
(set (keys (first yrel)))) | |
idx (index yrel ks)] | |
(reduce (fn [ret x] | |
(if-let [found (idx (select-keys x ks))] | |
(reduce #(conj %1 (merge %2 x)) ret found) | |
(conj ret x))) | |
#{} xrel)) | |
xrel)) | |
([xrel yrel km] | |
(let [idx (index yrel (vals km))] | |
(reduce (fn [ret x] | |
(if-let [found (idx (rename-keys (select-keys x (keys km)) km))] | |
(reduce #(conj %1 (merge %2 x)) ret found) | |
(conj ret x))) | |
#{} xrel))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
At line 12, I think it should be
(merge x %2)
instead of(merge %2 x)
(as in the original implementation).The reason for that is that you want values from
yrel
to "override" values inxrel
.Adapted example: