# Functions to manipulate ranges

**URL:** <https://discuss.kotlinlang.org/t/functions-to-manipulate-ranges/21571>\
**Category:** Libraries\
**Created:** [April 18, 2021, 12:03pm UTC](https://discuss.kotlinlang.org/t/functions-to-manipulate-ranges/21571 "2021-04-18T12:03:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmitry](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dmitry/32/10200_2.png) [@dmitry](https://discuss.kotlinlang.org/u/dmitry)\
**Post date:** [April 18, 2021, 12:03pm UTC](https://discuss.kotlinlang.org/t/functions-to-manipulate-ranges/21571/1 "2021-04-18T12:03:56Z")

</div>

Currently there are no methods to transform or combine ranges. Either you need to manually create a new range instance by coping all parameters, including the step of the `IntProgression`, even if the step didn’t change, either you need to unfold a range into an `Iterable` and use methods from there, but it’s not resource-efficient.

E.g.:  
`(1..5).shift(1)`  
result: `2..6`  
`(1..5).intersect(3..8)`  
result: `3..5`

---

<div class="post-metadata">

**Author:** ![Whathecode](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/whathecode/32/7494_2.png) [@Whathecode](https://discuss.kotlinlang.org/u/Whathecode)\
**Post date:** [July 25, 2024, 1:20pm UTC](https://discuss.kotlinlang.org/t/functions-to-manipulate-ranges/21571/2 "2024-07-25T13:20:31Z")

</div>

Kotlin ranges are fairly limited. Once you go down this path of adding seemingly trivial operations, you run into added complexity. E.g., adding/subtracting ranges will need a representation of unions of ranges. And, integers need to be treated differently from floating point types. You’ll start running into overflows and need to think about how to deal with that. And you may end up needing both lower and upper inclusive/exclusive bounds.

Having written a similar library in C# before which proved super useful in everyday tasks, I have started writing a similar library for kotlin doing the same: [Whathecode/kotlinx.interval: Kotlin multiplatform bounded open/closed generic intervals. (github.com)](https://github.com/Whathecode/kotlinx.interval)

This can be a useful reference to see the design considerations which may become relevant when tackling this. For now, I simply look at “intervals” as a separate thing than Kotlin ranges. Likely, it is a subset.
