Created
March 17, 2012 14:40
-
-
Save fbettag/2060143 to your computer and use it in GitHub Desktop.
Category Tree
This file contains hidden or 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
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