Suggestion: `ClosedRange<T>.mapBounds()` ``...
# stdlib
m
Suggestion:
ClosedRange<T>.mapBounds()
Copy code
val range = 1 .. 10
val shiftedRange = range.mapBounds { it + 10 } // = 11 .. 20
val characters = "a" .. "z"
val uppercaseCharacters = characters.mapBounds { it.toUpperCase() } // == "A" .. "Z"
Copy code
fun <T : Comparable<T>, R : Comparable<R>> ClosedRange<T>.mapBounds(transform: (T) -> R): ClosedRange<R> =
    transform(start) .. transform(endInclusive)

// plus overloads for primitive ranges
Note that due to KT-43981 this cannot be properly implemented at the moment for primitive boundary-producing transformations as it would break the generic case: https://youtrack.jetbrains.com/issue/KT-43981
a
How about
mapUpperBound
,
mapLowerBound
, and
mapBounds
which takes a pair?
m
A pair of what?
a
A pair of ints or chars. Depending on your generic type.
m
I want to map both ends of the range with a single function. No
Pair
involved.
a
Ah sorry. My fault
m
No worries, thanks for trying to help :)