Last active
March 16, 2017 17:16
-
-
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
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 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 | |
*/ |
Author
oldratlee
commented
Mar 16, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment