kirillrakhman
10/06/2016, 12:47 PMinline fun <T : Comparable<T>> ClosedRange<T>.toSequence(crossinline succ: T.() -> T): Sequence<T> {
if (start > endInclusive) return emptySequence()
return generateSequence(start) {
val next = it.succ()
if (next <= endInclusive) next else null
}
}
or even shorter but with more indirection:
fun <T : Comparable<T>> ClosedRange<T>.toSequence(succ: T.() -> T) = generateSequence(start, succ).takeWhile { it <= endInclusive }