Sam Stone
12/04/2022, 1:59 AMx in 0 until a
, return 0, a until b
return 1, b until c
return 2, greater than or equal to d
, return 3?Gleb Minaev
12/04/2022, 9:09 AMwhen(x) {
x in 0 until a -> return 0
x in a until b -> return 1
x in b until c -> return 2
x >= d -> return 3
}
But I think you should ask such questions in #getting-started. There is no specific mathematical context.Sam Stone
12/05/2022, 8:33 AMGleb Minaev
12/05/2022, 8:49 AMmap.getOrNull(key) ?: default
). IMO, replacing branching in this case is just making your life harder and your code less readable.
As for equivalent algorithm you can use
(min(a, x) - min(a-1, x) + min(b, x) - min(b-1, x) + 2 * (min(c, x) - min(c-1, x)) + min(d-1, x) - min(d, x)).let { if (it != 4) return it }
As for wrapping I can not come up with any idea.