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

elect

08/01/2022, 7:57 AM
I'd like to check if a range is within another range, ie
i-2..i+2 in wd.indices
e

ephemient

08/01/2022, 1:20 PM
there's two possible answers, both reasonable for different purposes:
Copy code
this.start <= other.endInclusive && this.endInclusive >= other.start
this.start >= other.start && this.endInclusive <= other.endInclusive
plus additional considerations around what the result should be with empty ranges
e

elect

08/01/2022, 1:23 PM
I don't see any reason why one would interpret this as the first option
r

Ruckus

08/01/2022, 2:48 PM
I - 2 in wd.indices && I + 2 in wd.indices
e

elect

08/01/2022, 2:49 PM
I'm doing exactly this atm
l

lhwdev

08/08/2022, 7:41 AM
Thinking how operator
in
is implemented in Kotlin, it seems to imply that some element is contained in some *set*(collection), not subset. In java, there is
containsAll
, not
contains
.
k

Klitos Kyriacou

08/08/2022, 8:03 AM
This would work but would currently be too expensive:
Copy code
wd.indices.toSet().containsAll((i-2..i+2).toSet())
That's only because toSet() creates a LinkedHashSet. Perhaps it could be optimized to create a specialized Set that just keeps the range as its only property and whose
contains
method just forwards to the range, and defines a suitably specialized
containsAll
. Then the above code would have good performance. Or perhaps just a new
containsAll
method on ranges.
l

lhwdev

08/08/2022, 8:05 AM
Yes, sorry for not being clear. I meant
contains
(which corresponds to
in
) does not work for set-to-set. (like
Set.contains(Set)
)
2 Views