william
07/07/2020, 2:44 PM>
, <=
, etc as an argument to a function?william
07/07/2020, 2:52 PM(Number, Number) -> Boolean)
as an arg but not sure about using >
directlyMargarita Bobova
07/08/2020, 5:59 AM> or <=
to a function like below or something else?
fun foo(a: Boolean) {}
fun main() {
val a = 5
val b = 6
foo(a >= b)
}
Michael de Kaste
07/08/2020, 8:07 AMfun main(){
foo(6,5){ a,b -> a > b}
}
fun foo(a: Int, b: Int, comparator: (Int, Int) -> Boolean) = comparator(a,b)
Matteo Mirk
07/08/2020, 9:26 AMcompareTo()
calls (https://kotlinlang.org/docs/reference/operator-overloading.html#comparison).
The shortest you can get is to declare comparison lambdas as variables and pass them:
fun test(x: Int, y: Int, f: (Int, Int) -> Boolean) = f(x, y)
val gt = { x: Int, y: Int -> x > y }
val gte = { x: Int, y: Int -> x >= y }
//etc...
fun main() {
test(2, 1, gt)
test(2, 2, gte)
}
william
07/08/2020, 4:10 PMcompareTo