Skip to content

Instantly share code, notes, and snippets.

@bts
Created March 3, 2015 21:21
Show Gist options
  • Save bts/bda3547ceb51f06d808a to your computer and use it in GitHub Desktop.
Save bts/bda3547ceb51f06d808a to your computer and use it in GitHub Desktop.
(defn topo-sort
"Provided a map of values to their dependencies, returns a seq of the values
sorted topologically.
Adapted from code by cgrande at:
https://groups.google.com/forum/#!topic/clojure/-sypb2Djhio"
[deps]
(mapcat #(for [[u vs] %
:when (empty? vs)]
u)
(take-while seq
(iterate (fn [deps]
(into {}
(for [[u vs] deps
:when (seq vs)]
[u (mapcat deps vs)])))
deps))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment