Skip to content

Instantly share code, notes, and snippets.

@Sedose
Created April 3, 2022 17:01
Show Gist options
  • Select an option

  • Save Sedose/bef6a04f5c7896e78005a1130d8dcb87 to your computer and use it in GitHub Desktop.

Select an option

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
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