Skip to content

Instantly share code, notes, and snippets.

@oldratlee
Last active March 16, 2017 17:16
Show Gist options
  • Save oldratlee/829b91987ce6e47f9d7feef5cb1ebacc to your computer and use it in GitHub Desktop.
Save oldratlee/829b91987ce6e47f9d7feef5cb1ebacc to your computer and use it in GitHub Desktop.
ScalaLinearization demo implementation by scala, based on https://gist.github.com/zavakid/05e18507ec3a311edcc87a4013b7d057
import scala.collection.immutable.ListSet
object ScalaLinearization {
case class Clazz(className: Symbol, superClasses: ListSet[Clazz])
// Ignore Any, AnyRef. Simplify demo implementation
val animal = Clazz('Animal, ListSet())
val furry = Clazz('Furry, ListSet(animal))
val hasLegs = Clazz('HasLegs, ListSet(animal))
val fourLegged = Clazz('FourLegged, ListSet(hasLegs))
val cat = Clazz('Cat, ListSet(animal, furry, fourLegged))
def linearization(clazz: Clazz): ListSet[Clazz] = clazz match {
case Clazz(_, supers) => supers.flatMap(linearization) + clazz
}
def main(args: Array[String]): Unit = {
for (clz <- List(animal, furry, hasLegs, fourLegged, cat)) {
println(f"${clz.className.name}%-10s : ${
linearization(clz).toIndexedSeq.reverse.map(_.className.name).mkString(", ")
}")
}
}
}
/*
Output:
Animal : Animal
Furry : Furry, Animal
HasLegs : HasLegs, Animal
FourLegged : FourLegged, HasLegs, Animal
Cat : Cat, FourLegged, HasLegs, Furry, Animal
*/
@oldratlee
Copy link
Author

6d743e5agy1fciyk8sdslj20ao0abgm4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment