https://kotlinlang.org logo
#random
Title
k

kirillrakhman

10/06/2016, 12:47 PM
the sequence once looks shorter:
Copy code
inline 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:
Copy code
fun <T : Comparable<T>> ClosedRange<T>.toSequence(succ: T.() -> T) = generateSequence(start, succ).takeWhile { it <= endInclusive }