Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scalaz._
- import Scalaz._
- case class Asset(id: Int, path: String)
- case class AssetDependencyChain(assetToDeps: Map[Asset, Set[Asset]]) {
- def getResolvedDependencies(startingAsset: Asset): ValidationNEL[String, Set[Asset]] = {
- def resolve(workingAsset: Asset, workingPath: Vector[Asset]): ValidationNEL[String, Set[Asset]] = {
- if (workingPath.contains(workingAsset)) "Circular dependency discovered with %s and along path %s.".format(workingAsset, workingPath).failNel[Set[Asset]]
- else {
- val workingSet = Set(workingAsset)
- val newWorkingPath = workingPath :+ workingAsset
- assetToDeps.get(workingAsset).map{dependencies =>
- dependencies.foldLeft(workingSet.successNel[String])((left, right) => (left <**> resolve(right, newWorkingPath))(_ ++ _))
- }.getOrElse(workingSet.successNel[String])
- }
- }
- resolve(startingAsset, Vector())
- }
- }
- object AssetTest extends App {
- val asset1 = new Asset(1, "Asset1")
- val asset2 = new Asset(2, "Asset2")
- val asset3 = new Asset(3, "Asset3")
- val validDepChain = new AssetDependencyChain(Map(asset1 -> Set(asset2), asset2 -> Set(asset3)))
- println(validDepChain.getResolvedDependencies(asset1))
- val invalidDepChain = new AssetDependencyChain(Map(asset1 -> Set(asset2), asset2 -> Set(asset3), asset3 -> Set(asset1)))
- println(invalidDepChain.getResolvedDependencies(asset1))
- }
Add Comment
Please, Sign In to add comment