当前位置:网站首页>[kotlin] Introduction to higher-order functions
[kotlin] Introduction to higher-order functions
2022-07-01 16:38:00 【Programmer Xiao He SS】
What is a higher-order function
A function that takes a function as an argument or returns a value , Called higher order function .
Define higher-order functions
action It's a function of higher order ,(Int) -> Int Indicates the type of the function ,(Int) Indicates that the input parameter type of the function is Int, hinder Int Indicates the return type of the function .
private fun init() {
val action: ((Int) -> Int) = {// Function to add 100 operation
it + 100
}
}
Function as parameter
The following code ,init Function call doSomething function , take Int type 0 as well as action Function is introduced to doSomething.doSomething The function first aligns the parameters 0 add 200 operation , Then we call the higher-order function. action( add 100 operation ), Final print results actionResult. Is it a bit of a strategic design pattern ? Yes , It can be used to implement the strategy design pattern .
private fun init() {
val action: ((Int) -> Int) = {// Function to add 100 operation
it + 100
}
doSomething(0, action)// take 0 as well as action Pass to doSomething
}
private fun doSomething(d: Int, action: (Int) -> Int) {
val data = d + 200// First pair d add 200 operation
val actionResult = action(data)// Will add 200 The result is passed to action, The results are stored in actionResult in
Log.e("test", "actionResult=${actionResult}")// Print the results
}
Function as return value
The following code , Depending on the type type, Return the corresponding action, Then the parameters 0 Carry out operations , Final print results .
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val action = init(" programme 1")
// Get the corresponding action, Pass in the parameter 0 Carry out operations
val result = action?.invoke(0)
// Print the results
Log.e("test", "result=${result}")
}
private fun init(type: String): ((Int) -> Int)? {
val action1: ((Int) -> Int) = {// Add 100 operation
it + 100
}
val action2: ((Int) -> Int) = {// Add 200 operation
it + 200
}
// According to the type type, Return the corresponding action
return when (type) {
" programme 1" -> action1
" programme 2" -> action2
else -> null
}
}
lamdba expression
It's also a higher-order function , It's just that the function is anonymous . The function of the following code is the same as the above , Just for amdba Expressions replace definitions action function .
private fun init() {
doSomething(0) { // take 0 as well as lambda The expression is passed to doSomething
it + 100// add 100 operation , And back to
}
}
private fun doSomething(d: Int, action: (Int) -> Unit) {
val data = d + 200// First pair d add 200 operation
val actionResult = action(data)// Will add 200 The result is passed to action, The results are stored in actionResult in
Log.e("test", "actionResult=${actionResult}")// Print the results
}
What is the essence of higher-order functions ?
Decompile the first example
private static final void init() {
Function1 action = new Function1() {//1
@Override
public Object invoke(Object o) {
return ((Integer)o).intValue() + 100;//2
}
};
doSomething(0, action);
}
private static final void doSomething(int d, Function1 action) {//3
int data = d + 200;
int actionResult = ((Number)action.invoke(data)).intValue();
Log.e("test", "actionResult=" + actionResult);
}
- notes 1:Kotlin The compiler converts higher-order functions into Function1;
- notes 2: Implementation of corresponding higher-order function ;
- notes 3: Call higher-order functions , In fact, the trigger is invoke function ;
There is something wrong with the above analysis , Please point out , learn from each other , Thank you !
边栏推荐
- Zabbix2.2监控之系统及应用日志监控报警
- 用手机在同花顺上开户靠谱吗?这样有没有什么安全隐患
- Zhou Shaojian, rare
- How to restore the system with one click on Lenovo laptop
- Principle of SSM framework
- 【Hot100】17. 电话号码的字母组合
- vim用户自动命令示例
- How to use phpipam to manage IP addresses and subnets
- Go language source level debugger delve
- How to solve the keyboard key failure of notebook computer
猜你喜欢

数据库系统原理与应用教程(003)—— MySQL 安装与配置:手工配置 MySQL(windows 环境)

德国iF多项大奖加冕,这副耳机有多强?音珀GTW 270 Hybrid深度评测

Dataframe gets the number of words in the string

Use Tencent cloud to build a map bed service

How to solve the problem that the battery icon of notebook computer does not display

VMware virtual machine failed during startup: VMware Workstation is incompatible with hyper-v

Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist

China's intelligent transportation construction from the perspective of "one hour life circle" in Dawan District

游戏行业安全选择游戏盾,效果怎么样?

Analysis of PostgreSQL storage structure
随机推荐
Bugku's file contains
怎么用MySQL语言进行行列装置?
高端程序员上班摸鱼指南
程序员职业生涯真的很短吗?
Installation and use of sqoop
Research on multi model architecture of ads computing power chip
Vscode find and replace the data of all files in a folder
Origin2018安装与使用(整理中)
Submission lottery - light application server essay solicitation activity (may) award announcement
Rhcsa Road
【Hot100】17. Letter combination of telephone number
Sweden announced its decision to exclude Huawei 5g equipment, but Huawei has successfully found a new way out
【Hot100】19. 删除链表的倒数第 N 个结点
Zhou Shaojian, rare
How to restore the system of Sony laptop
Zabbix2.2 monitoring system and application log monitoring alarm
Activity的生命周期和启动模式详解
Tutorial on the principle and application of database system (003) -- MySQL installation and configuration: manually configure MySQL (Windows Environment)
vim用户自动命令示例
数据库系统原理与应用教程(006)—— 编译安装 MySQL5.7(Linux 环境)