keturn12/08/2018, 10:13 PM
but that doesn't Do What I Mean because
fun isBetween(x, edge1, edge2): Boolean = x in (edge1 .. edge2)
is false. Is there anything in the standard library that handles this already, or do I have to add an
5 in (10 .. 1)
in there to make sure they're the right way around?
if edge2 < edge1
Davio12/09/2018, 5:56 AM
doesn't work because the rangeTo operator only works for incremental ranges
Alan Evans12/09/2018, 1:20 PM
fun isBetween(x: Int, edge1: Int, edge2: Int): Boolean = x in (edge1..edge2) || x in (edge2..edge1)
gildor12/09/2018, 2:27 PM
Alan Evans12/09/2018, 2:35 PM
I would side with readability, what would what you are suggesting look like, this?:
x in (minOf(a, b)..maxOf(a, b))
x >= minOf(a, b) && x <= maxOf(a, b)
gildor12/09/2018, 2:45 PM
Alan Evans12/09/2018, 2:50 PM
gildor12/09/2018, 2:51 PM
Alan Evans12/09/2018, 2:54 PM
should be no less efficient than
x in (edge1..edge2)
. If it is less efficient, that's a compiler failure IMO.
x >= edge1 && x <= edge2
and if we can't without a cost, that's on Kt's compiler team.
gildor12/09/2018, 3:11 PM
Alan Evans12/09/2018, 3:28 PM
, for two static method invocations. If you want to talk efficiency, I think this is a case of profiling over theorizing (which is almost always the case).
Creating 2 ranges (or even one range as in original snippet) just to check range looks as overkill for meThis statement is false. There is no "creation" going on. The compiler seems to handle it quite efficiently. The IDE will even recommend it.
gildor12/09/2018, 4:41 PM
Alan Evans12/09/2018, 6:35 PM