- Reactive Programming in Kotlin
- Rivu Chakraborty
- 155字
- 2021-07-02 22:26:39
High-order functions
Those functions that take another function as an argument or return a function as result are called high-order functions. Consider the following example to understand it better:
fun highOrderFunc(a:Int, validityCheckFunc:(a:Int)->Boolean) {//(1) if(validityCheckFunc(a)) {//(2) println("a $a is Valid") } else { println("a $a is Invalid") } } fun main(args: Array<String>) { highOrderFun(12,{ a:Int -> a.isEven()})//(3) highOrderFunc(19,{ a:Int -> a.isEven()}) }
In this program, we've declared a highOrderFunc function, which will take an Int and a validityCheckFunc(Int) function. We are calling the validityCheckFunc function inside the highOrderFunc function, to check whether the value was valid or not. However, we are defining the validityCheckFunc function at runtime, while we are calling the highOrderFunc function inside the main function.
Note that the isEven function in this program is an extension function that has been defined inside the project files you got with the book.
Here is the output:
a 12 is Valid a 19 is Invalid
推薦閱讀
- 算法競賽入門經典:習題與解答
- 云計算環境下的信息資源集成與服務
- Voice Application Development for Android
- Redis應用實例
- 數據庫開發實踐案例
- 達夢數據庫性能優化
- 白話大數據與機器學習
- SQL優化最佳實踐:構建高效率Oracle數據庫的方法與技巧
- 云原生數據中臺:架構、方法論與實踐
- 計算機應用基礎教程上機指導與習題集(微課版)
- Mastering LOB Development for Silverlight 5:A Case Study in Action
- R Machine Learning Essentials
- 數據庫原理與設計實驗教程(MySQL版)
- 算法設計與分析
- 2D 計算機視覺:原理、算法及應用