Introduce
Articles with the topic Higher Order Functions for those new to Kotlin.
If you are an Android developer coming from Java, I will definitely comment that Kotlin is really “cool”. One of the things that makes this magic is the functional programming support. That is, functions can be passed as a variable of another function, and can also be returned from a certain function. That is the Higher Order Function.
Definition of a Higher Order Function
Let’s take a look at the following basic example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | //A simple Higher Order Function in Kotlin //This function accepts three parameters //And the last parameter is a function fun rollDice( range: IntRange, time: Int, callback: (result: Int) -> Unit ) { for (i in 0 until time) { val result = range.random() //As the last parameter is a function //we can call it as a function callback(result) } } |
Of the three parameters passed to the rollDice function, the third parameter is a function
In there, the callback
is the name of the functions, and the name of the parameter passed. (result: Int)
again the parameter of the callback
. Unit
is the return type of the function. In this case, it doesn’t need to return anything.
Next we call the Higher Order Function.
1 2 3 4 5 6 | fun main() { rollDice(1..6, 3, { result -> println(result) }) } |
Here, result is the parameter of the callback function. The println command is the work done corresponding to the above callback function.
See practical example
Here we come to a few lines of code offline.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class ExampleDialog( context: Context, private val onProcessClick: (dialog: Dialog) -> Unit ) : BaseDialog(context = context) { override val isCancelable: Boolean get() = true override fun initView() { this.setContentView(R.layout.example_dialog) val button = this.findViewById<Button>(R.id.process_btn) button.setSingleClick { dismiss() onProcessClick(this) } } } |
Where you want to show Dialog.
1 2 3 4 5 | ExampleDialog(requireContext()) { doAnyThing() //this func will implement when onProcessClick(this) is called }.show() |
Add another example, also ExampleDialog
above but with 2 callbacks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class ExampleDialog( context: Context, private val onPreventClick: (dialog: Dialog) -> Unit private val onProcessClick: (dialog: Dialog) -> Unit ) : BaseDialog(context = context) { override val isCancelable: Boolean get() = true override fun initView() { this.setContentView(R.layout.example_dialog) val button = this.findViewById<Button>(R.id.process_btn) button.setSingleClick { dismiss() onProcessClick(this) } val button01 = this.findViewById<Button>(R.id.prevent_btn) button01.setSingleClick { dismiss() onPreventClick(this) } } } |
And here is the function call Dialog:
1 2 3 4 5 6 | ExampleDialog(requireContext(), { doPreventClickHandle() }, { doProcessClickHandle() }).show() |
Hope the above examples will give you more practical insight about Higher Order Function. Not only are they useful, but they’re also cool, aren’t they !!!