Created
June 16, 2019 12:28
-
-
Save automationhacks/b61391d486919d9f3ccf8cfe3dd018aa to your computer and use it in GitHub Desktop.
Converting if else to a when
This file contains hidden or 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
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