Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save flengyel/1436583 to your computer and use it in GitHub Desktop.
Save flengyel/1436583 to your computer and use it in GitHub Desktop.
;; flengyel's solution to Transitive Closure
;; https://4clojure.com/problem/84
(fn [rel]
(reduce clojure.set/union
(take (count rel)
(iterate
(partial
(fn [R S]
(set (let [left (lazy-seq (map #(% 0) R))
right (lazy-seq (map #(% 1) S))
field (lazy-cat (map #(% 1) R) (map #(% 0) S))]
(keep (fn [[i k j]] (if (and (contains? R [i k])
(contains? S [k j]))
[i j]
nil))
(for [i left j right k field] [i k j])))))
rel) rel))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment