https://kotlinlang.org logo
#stdlib
Title
# stdlib
e

Eugen Martynov

12/05/2023, 10:36 AM
Surprised there is no intersect for the Range. Probably because there is case when no intersection is found. But would go probably for intersectOrNull
e

ephemient

12/05/2023, 10:46 AM
Copy code
fun IntRange.intersect(other: IntRange): IntRange =
    maxOf(this.first, other.first)..minOf(this.last, other.last)
may result in an empty range but that's probably better than
null
e

Eugen Martynov

12/05/2023, 10:47 AM
I thought that it will throw exception in case of first above last
e

ephemient

12/05/2023, 10:49 AM
nope
e

Eugen Martynov

12/05/2023, 10:49 AM
Oke, cool
e

ephemient

12/05/2023, 10:49 AM
(1..0).isEmpty()
=>
true
j

Joffrey

12/05/2023, 1:40 PM
@ephemient how does that work for ranges with negative step?
e

ephemient

12/05/2023, 1:42 PM
steps are in e.g. IntProgression not Range
j

Joffrey

12/05/2023, 1:42 PM
Ah, good point. It seems I haven't played enough with these things 😄