Created
October 10, 2012 14:09
-
-
Save jonasabreu/3865860 to your computer and use it in GitHub Desktop.
Código que quase resolve o problema do Dojo 5 em Scala (não resolve por que é possível especificar as relações como A(B()), B(C()). Vai produzir a saída errada para esse caso). Se a preguiça passar, eu termino ele :)
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
import org.specs2.mutable.Specification | |
import org.junit.runner.RunWith | |
import org.specs2.runner.JUnitRunner | |
import org.junit.runner.RunWith | |
import org.specs2.runner.JUnitRunner | |
@RunWith(classOf[JUnitRunner]) | |
class DepsSpec extends Specification { | |
import Deps._ | |
"Dependencias" should { | |
"devolver A B quando A depende de B" in { | |
A(B()).dependencies must_== List("A B") | |
} | |
"devolver A C quando A depende de C" in { | |
A(C()).dependencies must_== List("A C") | |
} | |
"devolver A B C quando A depende de B e C" in { | |
A(B(), C()).dependencies must_== List("A B C") | |
} | |
"devolver A B C quando A depende de C e B" in { | |
A(C(), B()).dependencies must_== List("A B C") | |
} | |
"devolver A B C e B C quando A depende de B que depende de C" in { | |
A(B(C())).dependencies must_== List("A B C", "B C") | |
} | |
"devolver A B C e C B quando A depende de C que depende de B" in { | |
A(C(B())).dependencies must_== List("A B C", "C B") | |
} | |
"devolver A B e C D quando A depende de B e C depende de D" in { | |
List(A(B()), C(D())).dependencies must_== List("A B", "C D") | |
} | |
"devolver A B C D, B C D e C D" in { | |
val c = C(D()) | |
List(A(B(c)), c).dependencies must_== List("A B C D", "B C D", "C D") | |
} | |
} | |
} | |
object Deps { | |
implicit def listDeps(list : List[Item]) : { def dependencies : List[String] } = new { | |
def dependencies : List[String] = list.flatMap(_.dependencies).sortWith(_ < _).distinct | |
} | |
} | |
abstract class Item(val deps : List[Item]) { | |
def dependencies : List[String] = | |
List(this + " " + transitiveDeps.mkString(" ")) ++ deps.filter(_.deps.size > 0).flatMap(_.dependencies) | |
def transitiveDeps : List[Item] = | |
deps.flatMap(item => List(item) ++ item.transitiveDeps).sortWith(_.toString < _.toString) | |
override def toString = this.getClass.getSimpleName | |
} | |
class A(deps : List[Item]) extends Item(deps) | |
class B(deps : List[Item]) extends Item(deps) | |
class C(deps : List[Item]) extends Item(deps) | |
class D(deps : List[Item]) extends Item(deps) | |
abstract class ItemBuilder(f : List[Item] => Item) { | |
def apply(deps : Item*) = f(deps.toList) | |
} | |
object A extends ItemBuilder(new A(_)) | |
object B extends ItemBuilder(new B(_)) | |
object C extends ItemBuilder(new C(_)) | |
object D extends ItemBuilder(new D(_)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Scala é louco mesmo!