Skip to content

Instantly share code, notes, and snippets.

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