Skip to content

Instantly share code, notes, and snippets.

@fbettag
Created March 17, 2012 14:40
Show Gist options
  • Save fbettag/2060143 to your computer and use it in GitHub Desktop.
Save fbettag/2060143 to your computer and use it in GitHub Desktop.
Category Tree
case class Category(name: String, treeName: String, slug: String, numEntries: Int = 0, categories: List[Category] = List()) {
def apply(x: Category): Category = {
this // merge x into this.categories to get a Tree-structure
}
}
case class CategoryTree(groups: Groups) {
val list: List[Category] = {
var res: Map[String, Category] = Map()
groups.groups.map(g => {
val splitC = g.groupValue.split(" / ")
val newSubCat = Category(splitC.last, g.groupValue, Item.slugify(g.groupValue), g.numFound)
val newCat = res.get(splitC.head) match {
case Some(x: Category) => x(newSubCat)
case _ => Category(splitC.head, splitC.head, Item.slugify(splitC.head), g.numFound)(newSubCat)
}
res = res ++ Map(splitC.head -> newCat)
})
res.map(_._2).toList
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment