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

Hullaballoonatic

07/05/2020, 9:33 PM
Copy code
fun <C : Comparable<C>> C.coerceAtMost(maxValue: C) = if (this > maxValue) maxValue else this
fun <C : Comparable<C>> C.coerceAtLeast(minValue: C) = if (this < minValue) minValue else this
fun <C : Comparable<C>> C.coerceIn(minValue: C, maxValue: C) = coerceAtLeast(minValue).coerceAtMost(maxValue)
Also
Copy code
fun <C : Comparable<C>> min(vararg elements: C): C
fun <C : Comparable<C>> max(vararg elements: C): C
I'm a bit surprised these methods aren't part of stdlib of any languages i know of. Is there a reason I haven't considered?
h

Hullaballoonatic

07/07/2020, 11:13 PM
i don't understand how i somehow missed it when i'm running 1.4-M3 and it's been in since 1.3 maybe i wasn't using comparable classes on accident... idk
2 Views