Created
November 6, 2011 04:20
-
-
Save dth/1342467 to your computer and use it in GitHub Desktop.
scala fun
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
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))) | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output:
research
table
table-row
table-cell
table-val
author
balancesheet