Last active
January 7, 2017 01:34
-
-
Save zavakid/05e18507ec3a311edcc87a4013b7d057 to your computer and use it in GitHub Desktop.
scala linearizated
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
object LinearizationLearn { | |
case class Clazz(name: String, succ: List[Clazz]) | |
val animal = Clazz("animal", Nil) | |
val furry = Clazz("furry", List(animal)) | |
val hasLegs = Clazz("hasLegs", List(animal)) | |
val fourLegged = Clazz("fourLegged", List(hasLegs)) | |
val cat = Clazz("cat", List(animal, furry, fourLegged)) | |
def linearizate(clazz: Clazz): List[String] = { | |
def f(acc: List[String], clz: Clazz): List[String] = clz match { | |
case Clazz(name, list) => insertIfNotDuplicated(name, list.foldLeft(acc)(f)) | |
} | |
def insertIfNotDuplicated(elem: String, list: List[String]): List[String] = | |
(List(elem) diff list) ::: list | |
f(Nil, clazz) | |
} | |
def main(args: Array[String]): Unit = { | |
println(linearizate(animal)) | |
println(linearizate(furry)) | |
println(linearizate(hasLegs)) | |
println(linearizate(fourLegged)) | |
println(linearizate(cat)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment