Skip to content

Instantly share code, notes, and snippets.

@dth
Created November 6, 2011 04:20
Show Gist options
  • Save dth/1342467 to your computer and use it in GitHub Desktop.
Save dth/1342467 to your computer and use it in GitHub Desktop.
scala fun
case class Item(cat: String, parent: Option[String], items: Seq[Map[String,String]])
val rows = Seq(
Map("a1" -> "b1"),
Map("a2" -> "b2"),
Map("a3" -> "b3")
)
val items = List(
Item("research", None, rows),
Item("table", Some("research"), rows),
Item("table-row", Some("table"), rows),
Item("balancesheet", None, rows),
Item("table-val", Some("table-cell"), rows),
Item("table-cell", Some("table-row"), rows),
Item("author", Some("research"), rows)
)
def process(parent: Item, children: Seq[Item]) {
println(parent.cat)
children.foreach(item => {
process(item, items.filter(_.parent.getOrElse(None).equals(item.cat)))
})
}
items.filter(_.parent == None).foreach(item => {
process(item, items.filter(i => i.parent.getOrElse(None).equals(item.cat)))
})
@dth
Copy link
Author

dth commented Nov 7, 2011

output:

research
table
table-row
table-cell
table-val
author
balancesheet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment