I'm reading the kotlin tutorial in codelads and this question came up, because in the codelabs they show us examples but not one of this type, so what I want is something like this:
var dirtyLevel = 20
val waterFilter: (Int) -> Int = { dirty -> dirty / 2}// funcion lambda
val updateDirty: Int = { dirty : Int, operation : (Int) -> Int -> operation(dirty)}// funcion lambda de orden superior
println(waterFilter(dirtyLevel))
println(updateDirty(30,waterFilter))
generates this error:
error: type mismatch: inferred type is (Int, (Int) -> Int) -> Int but Int was expected
val updateDirty: Int = { dirty : Int, operation : (Int) -> Int -> operation(dirty)}
^
I also tried it like val updateDirty: Int = { (dirty : Int, operation : (Int) -> Int) -> operation(dirty)}
this but it generates this error:
error: type mismatch: inferred type is (???) -> [ERROR : <ERROR FUNCTION RETURN TYPE>] but Int was expected
val updateDirty: Int = { (dirty : Int, operation : (Int) -> Int) -> operation(dirty)}
The syntax would be the following:
That is, we define a function called updateDirty, which accepts two parameters, the first of type integer (A), this is where we pass the 30, and the second parameter is a function that accepts an integer parameter and returns another integer ( B). This is where you pass waterFilter to it.
Now for the implementation of the function, we indicate that it has two parameters: num (D), and y operation (E). And what the content of the function (F) does is call the function that we pass as a parameter in our case waterFilter, passing as a parameter the number that we pass to it, in our case 30.