Guest User

Untitled

a guest
Jan 21st, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. import scalaz._
  2. import Scalaz._
  3.  
  4. case class Asset(id: Int, path: String)
  5. case class AssetDependencyChain(assetToDeps: Map[Asset, Set[Asset]]) {
  6. def getResolvedDependencies(startingAsset: Asset): ValidationNEL[String, Set[Asset]] = {
  7. def resolve(workingAsset: Asset, workingPath: Vector[Asset]): ValidationNEL[String, Set[Asset]] = {
  8. if (workingPath.contains(workingAsset)) "Circular dependency discovered with %s and along path %s.".format(workingAsset, workingPath).failNel[Set[Asset]]
  9. else {
  10. val workingSet = Set(workingAsset)
  11. val newWorkingPath = workingPath :+ workingAsset
  12. assetToDeps.get(workingAsset).map{dependencies =>
  13. dependencies.foldLeft(workingSet.successNel[String])((left, right) => (left <**> resolve(right, newWorkingPath))(_ ++ _))
  14. }.getOrElse(workingSet.successNel[String])
  15. }
  16. }
  17. resolve(startingAsset, Vector())
  18. }
  19. }
  20.  
  21. object AssetTest extends App {
  22. val asset1 = new Asset(1, "Asset1")
  23. val asset2 = new Asset(2, "Asset2")
  24. val asset3 = new Asset(3, "Asset3")
  25. val validDepChain = new AssetDependencyChain(Map(asset1 -> Set(asset2), asset2 -> Set(asset3)))
  26. println(validDepChain.getResolvedDependencies(asset1))
  27. val invalidDepChain = new AssetDependencyChain(Map(asset1 -> Set(asset2), asset2 -> Set(asset3), asset3 -> Set(asset1)))
  28. println(invalidDepChain.getResolvedDependencies(asset1))
  29. }
Add Comment
Please, Sign In to add comment