Skip to content

Instantly share code, notes, and snippets.

@matklad
Created December 24, 2016 21:03
Show Gist options
  • Save matklad/54776705250e3b375618f59a8247a237 to your computer and use it in GitHub Desktop.
Save matklad/54776705250e3b375618f59a8247a237 to your computer and use it in GitHub Desktop.
An "inclusive" version of Kotlin's takeWhile
fun <T> Sequence<T>.takeWhileInclusive(pred: (T) -> Boolean): Sequence<T> {
var shouldContinue = true
return takeWhile {
val result = shouldContinue
shouldContinue = pred(it)
result
}
}
@wafisher
Copy link

I love this. If Sequence were parallel, though, wouldn't there be worries about using out-of-closure state?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment