keturn
12/08/2018, 10:13 PMfun isBetween(x, edge1, edge2): Boolean = x in (edge1 .. edge2)
but that doesn't Do What I Mean because 5 in (10 .. 1)
is false.
Is there anything in the standard library that handles this already, or do I have to add an if edge2 < edge1
in there to make sure they're the right way around?Davio
12/09/2018, 5:56 AM(10..1)
doesn't work because the rangeTo operator only works for incremental rangesAlan Evans
12/09/2018, 1:20 PMfun isBetween(x: Int, edge1: Int, edge2: Int): Boolean = x in (edge1..edge2) || x in (edge2..edge1)
gildor
12/09/2018, 2:27 PMAlan Evans
12/09/2018, 2:35 PMx in (minOf(a, b)..maxOf(a, b))
I would side with readability, what would what you are suggesting look like, this?:
x >= minOf(a, b) && x <= maxOf(a, b)
gildor
12/09/2018, 2:45 PMAlan Evans
12/09/2018, 2:50 PMc
.gildor
12/09/2018, 2:51 PMAlan Evans
12/09/2018, 2:54 PMx in (edge1..edge2)
should be no less efficient than x >= edge1 && x <= edge2
. If it is less efficient, that's a compiler failure IMO.in
and if we can't without a cost, that's on Kt's compiler team.gildor
12/09/2018, 3:11 PMAlan Evans
12/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.
gildor
12/09/2018, 4:41 PMAlan Evans
12/09/2018, 6:35 PM