Skip to content

Instantly share code, notes, and snippets.

@chrhicks
Created February 26, 2013 15:10
Show Gist options
  • Save chrhicks/5039126 to your computer and use it in GitHub Desktop.
Save chrhicks/5039126 to your computer and use it in GitHub Desktop.
Traversing a handlebars.scala Program
object ProgramHelper {
def filter(node: Node)(implicit filterFn: Node => Boolean): List[Node] = {
(if (filterFn(node)) List(node) else List()) ++ (node match {
case n:Path => n.value.flatMap(filter(_))
case n:Partial => filter(n.value)
case n:Mustache => filter(n.value) ++ n.parameters.flatMap(filter(_))
case n:Section => filter(n.name) ++ filter(n.value)
case n:Program => n.value.flatMap(filter(_)) ++ n.inverse.map(filter(_)).getOrElse(List())
case _ => List()
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment