Last active
December 2, 2016 02:11
-
-
Save kakajika/9dbe230c57acb0769cd4c0579b53c0cc to your computer and use it in GitHub Desktop.
java.io.Reader extensions for doing something with each chars in Kotlin.
This file contains 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 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