Skip to content

Instantly share code, notes, and snippets.

@zavakid
Last active January 7, 2017 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zavakid/05e18507ec3a311edcc87a4013b7d057 to your computer and use it in GitHub Desktop.
Save zavakid/05e18507ec3a311edcc87a4013b7d057 to your computer and use it in GitHub Desktop.
scala linearizated
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