Skip to content

Instantly share code, notes, and snippets.

@royaldark
Created July 20, 2018 22:42
Show Gist options
  • Save royaldark/8cc4910b72fdb389a8fbf83e7cb0e5b2 to your computer and use it in GitHub Desktop.
Save royaldark/8cc4910b72fdb389a8fbf83e7cb0e5b2 to your computer and use it in GitHub Desktop.
(def idx-a {:a [[:a 1] [:a 2]]
:b [[:b 3]]
:c [[:c 4]]
:d [[:d 2]]})
(def idx-b {1 [[:a 1]]
2 [[:a 2] [:d 2]]
3 [[:b 3]]
4 [[:c 4]]})
(defn process-group
[idx-a idx-b starting-k]
(loop [cur-idx idx-a
nxt-idx idx-b
lookup-fns (cycle [second first])
k's #{starting-k}
group #{}]
(let [at-k (apply concat (vals (select-keys cur-idx k's)))
new-group (apply conj group at-k)]
(println "cur idx:" cur-idx)
(println "nxt idx:" nxt-idx)
(println "k's:" k's)
(println "at-k:" at-k)
(println "group:" group)
(cond
;; Base case: no values were found by looking up all keys in the current
;; index, meaning there are no more values to process
(empty? at-k)
new-group
;; Recursive case: add all found values from the current index to the
;; group, remove all used keys from the current index, swap the indices,
;; and repeat
:else
(recur nxt-idx
(apply dissoc cur-idx k's)
(rest lookup-fns)
(into #{} (map (first lookup-fns) new-group))
(apply conj new-group at-k))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment