ClosedRange to Sequence

Hello,

What do you think is it worth adding the following extension to the Kotlin Library?

fun <T> ClosedRange<T>.toSequence(nextItem: (T) -> (T)) : Sequence<T> {
    generateSequence(nextItem.start) { prev ->
        val next = nextItem(prev)
        when {
            next <= endInclusive -> next
            else -> null
        }
     }
}

Usage:

fun ClosedRange<LocalDate>.getDays(): Sequence<LocalDate> {
      return toSequence {
           plusDays(1)
      }
}

KT-40934

Your specific example doesn’t look very convincing, since toSequence must evaluate the next element before each range check:

// perfectly valid range, 
// throws DateTimeException because of LocalDate.MAX.plusDays(1)
(LocalDate.MAX..LocalDate.MAX).getDays().forEach {}