Search code examples
kotlinrange

Is it possible to concatenate two ranges in Kotlin?


I would like to do something like

for (i in 1..8,20..25)

or

for (i in 1..8 + 20..25)

Is it or something similar allowing to iterate over or check that an element is in a concatenated range possible in Kotlin?


Solution

  • Perhaps something like this?

    for (i in (1..8) + (20..25)) {
        println(i)
    }
    

    prints: 1 2 3 4 5 6 7 8 20 21 22 23 24 25

    You can try it here: https://pl.kotl.in/-0kdY_y5U?theme=darcula