Interesting Kotlin Technique: High Order Function(Function as a Parameter/ Return a Function)
In Kotlin, you can call another function in a function’s parameter.
— +++ Menu +++ —
🙈1. No Parameter
🙉2. With Parameter(s)
🐹3. Test with 1 parameter
🎎4. Test with 2 parameters
🙈1. No Parameter
< === Menu
For example, I create a function called
compareInput():
import kotlin.reflect.KFunction0// compare input and max
// true: call refA
// false: call refB
fun compareInput (
input: Int,
max: Int,
refA: KFunction0<Unit>,
refB: KFunction0<Unit>
) {
if (input > max)
refA()
else
refB()
}
Easy, right?
Let’s call some functions:
fun showA() { println("Show content A") }
fun showB() { println("Show content B") }
fun showC() { println("Show content C") }
fun showD() { println("Show content D") }
I can call different results from their references.
fun main(args: Array<String>) {
compareInput( 1, 3, ::showA, ::showB )
compareInput( 4, 3, ::showA, ::showB )
compareInput( -2, 3, ::showC, ::showD )
compareInput( 5, 3, ::showC, ::showD )
}
My output:
Show content B
Show content A
Show content D
Show content C
🙉2. With Parameter(s)
< === Menu
The parameter is easy to add.
You have learned that 0 parameter is:
import kotlin.reflect.KFunction0KFunction0<Unit>
1 parameter:
import kotlin.reflect.KFunction1KFunction1<String, Unit>
2 parameters:
import kotlin.reflect.KFunction2KFunction2<String, Customer, Unit>
Don’t you see the pattern?
🐹3. Test with 1 parameter
< === Menu
I mix 0 parameter function and 1 parameter function:
import kotlin.reflect.KFunction1
import kotlin.reflect.KFunction0// compare input and max
// true: call refA
// false: call refB
fun compareInput (
input: Int,
max: Int,
refA: KFunction1<String, Unit>,
refB: KFunction0<Unit>
) {
if (input > max)
refA(input.toString())
else
refB()
}
Same, I call four functions:
fun showA(winner:String) { println("A: Wining # $winner") }
fun showB() { println("Max is the winner") }
fun showC(winner:String) { println("C set: My winner is $winner") }
fun showD() { println("Winner is Max.") }
The main() is the same. Output as:
Max is the winner
A: Wining # 4
Winner is Max.
C set: My winner is 5
Working fine.
🎎4. Test with 2 parameters
< === Menu
I mix 1 parameter function and 2 parameters function:
import kotlin.reflect.KFunction1
import kotlin.reflect.KFunction2// compare input and max
// true: call refA
// false: call refB
fun compareInput (
input: Int,
max: Int,
refA: KFunction1<String, Unit>,
refB: KFunction2<Int, String, Unit>
) {
if (input > max)
refA(input.toString())
else
refB(input, "failed")
}
Functions:
fun showA(winner:String) { println("A: Wining # $winner") }
fun showB(input:Int, msg:String) {
println("Input is the $input. It's $msg.")
}fun showC(winner:String) { println("C set: My winner is $winner") }
fun showD(input:Int, msg:String) {
println("Input($input) has $msg the competition.")
}
Same main(). Output as:
Input is the 1. It's failed.
A: Wining # 4
Input(-2) has failed the competition.
C set: My winner is 5
All of them are working great!
😅, don’t you like to use this type of reflection?
Try it yourself!
This technique can save you a lot of codes.
More Example: