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
def tsort[A](edges: Traversable[(A, A)]): Iterable[A] = { | |
@tailrec | |
def tsort(toPreds: Map[A, Set[A]], done: Iterable[A]): Iterable[A] = { | |
val (noPreds, hasPreds) = toPreds.partition { _._2.isEmpty } | |
if (noPreds.isEmpty) { | |
if (hasPreds.isEmpty) done else sys.error(hasPreds.toString) | |
} else { | |
val found = noPreds.map { _._1 } | |
tsort(hasPreds.mapValues { _ -- found }, done ++ found) | |
} |