(defn topo-comparator
"Returns a comparator fn which produces a topological sort based on
the dependencies in graph. Nodes not present in the graph will sort
after nodes in the graph."
[graph]
(let [pos (zipmap (topo-sort graph) (range))]
(fn [a b]
(compare (get pos a Long/MAX_VALUE)
(get pos b Long/MAX_VALUE)))))