Skip to content

Instantly share code, notes, and snippets.

@automationhacks
Created June 16, 2019 12:30
Show Gist options
  • Save automationhacks/4a8bcec0ae180affea3a71cf99d31461 to your computer and use it in GitHub Desktop.
Save automationhacks/4a8bcec0ae180affea3a71cf99d31461 to your computer and use it in GitHub Desktop.
Traverse code with use forEach and method references
fun Element.extractText(): String {
val sb = StringBuilder()
fun extractText(e: Element) {
when (e) {
is Text -> sb.append(e.text)
is Container -> {
e.children.forEach(::extractText)
}
else -> error("Unrecognized element : $e")
}
}
extractText(this)
return sb.toString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment