Skip to content

Instantly share code, notes, and snippets.

@seivan
Created June 8, 2016 09:19
Show Gist options
  • Save seivan/321171a17b69d7c4296b78e627b7cb10 to your computer and use it in GitHub Desktop.
Save seivan/321171a17b69d7c4296b78e627b7cb10 to your computer and use it in GitHub Desktop.
let keys = [
1 : ["value" : "FirstValue", "dependencies" : Set(arrayLiteral: 2)],
2 : ["value" : "SecondValue", "dependencies" : Set(arrayLiteral: 1)],
]
keys.reduce(Dictionary<Set<Int>, Set<Int>>()) { (allDependencies, keyValueTuple) in
let (key, content) = keyValueTuple
var dependencyGraph = allDependencies
let dependencyKey = (content["dependencies"] as! Set<Int>).union([key])
dependencyGraph[dependencyKey] = dependencyGraph[dependencyKey]?.union([key]) ?? [key]
if dependencyGraph[dependencyKey] == dependencyKey {
print("Dependency has been fulfilled for \(key) with list \(dependencyGraph[dependencyKey])")
}
else {
print("Dependency is not fulfilled yet for key: \(key)")
}
return dependencyGraph
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment