-
-
Save Sedose/bef6a04f5c7896e78005a1130d8dcb87 to your computer and use it in GitHub Desktop.
Koltin map string to sequence of chars with previous ones. #kotlin loop with previous
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 String.withPrevious(): Sequence<Element> { | |
| val string = this | |
| return sequence { | |
| for (i in string.indices) { | |
| val current = string[i] | |
| val previous = if (i > 0) string[i - 1] else null | |
| yield(Element(current, previous)) | |
| } | |
| } | |
| } | |
| data class Element ( | |
| val current: Char, | |
| val previous: Char? | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment