Skip to content

Instantly share code, notes, and snippets.

@kakajika
Last active December 2, 2016 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kakajika/9dbe230c57acb0769cd4c0579b53c0cc to your computer and use it in GitHub Desktop.
Save kakajika/9dbe230c57acb0769cd4c0579b53c0cc to your computer and use it in GitHub Desktop.
java.io.Reader extensions for doing something with each chars in Kotlin.
fun Reader.forEachChars(block: (Char) -> Unit) {
while (true) {
val c = read()
if (c != -1) {
block(c.toChar())
} else return
}
}
fun <R> Reader.mapEachChars(transform: (Char) -> R): List<R> {
return mutableListOf<R>().apply {
forEachChars {
add(transform(it))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment