当前位置:网站首页>Several ways of lambda used in functions in kotlin (higher-order functions)
Several ways of lambda used in functions in kotlin (higher-order functions)
2022-07-07 08:23:00 【yu-Knight】
//Kotlin Used in functions in lambda Several ways
fun main(args: Array<String>) {
/**
* The return type in the function has lambda
*/
// First step : Declaration of function input and output The second step : Implementation of declarative functions
val methodAction1: (Int, Int, Int) -> String = { number1, number2, number3 ->
val inputValue = 99999
"$inputValue yuknight Parameter one :$number1, Parameter two :$number2, Parameter 3 :$number3"
}
// The third step : Call this function
println(methodAction1(2, 4, 6))
/*----------------------------------------------------*/
val methodAction4: (String) -> String = { "$it yuknight" }
println(methodAction4("kotlin"))
/*----------------------------------------------------*/
// Type inference of anonymous functions
// Anonymous functions : The type is inferred as String
// Method name : Must specify Parameter type and Return type
// Method name = Type inference return type
val methodAction6 = { v1: Double, v2: Float, v3: Int ->
"v1:$v1, v2:$v2, v3:$v3"
}//methodAction6 function :(Double, Float, Int)->String
println(methodAction6(22.3, 34.3f, 77))
val method1 = { 3.14f } //method1 function :()->Float
println(method1())
val method2 = { number: Int -> number }//method2 function :(Int)->Int
println(method2(88))
/*----------------------------------------------------*/
/**
* There are in the parameters of the function lambda
*/
//lambda The return value of the function is String
val serverResponse1 = { responseMsg: String, responseCode: Int ->
println(
"lambda The return value of the function is String The result is :responseMsg:$responseMsg, responseCode:$responseCode"
)
"lambda The return value of the function is String The result is :responseMsg:$responseMsg, responseCode:$responseCode"
// println(" The final result of getting the server response is :responseMsg:$responseMsg, responseCode:$responseCode")
}
doLogin11("yuknight", "123456", serverResponse1)
/*----------------------------------------------------*/
doLogin11("yuknight", "123456") { responseMsg: String, responseCode: Int ->
println("lambda The return value of the function is String The result is :responseMsg:$responseMsg, responseCode:$responseCode")
"lambda The return value of the function is String The result is :responseMsg:$responseMsg, responseCode:$responseCode"
}
/**
* There are lambda
*/
val lambdaActionFun = { msg: String, code: Int ->
"msg:$msg, code:$code"
}
val lambdaActF = lambdaFun("yuknight", 200, lambdaActionFun)
val lambdaStrF = lambdaActF(" Return value ", 300)
println("lambdaStr:$lambdaStrF")
//
val lambdaAct = lambdaFun("yuknight", 200) { msg: String, code: Int ->
"msg:$msg, code:$code"
}
val lambdaStr = lambdaAct(" Return value ", 300)
println("lambdaStr:$lambdaStr")
//
val lambdaFunStr = lambdaFun("yuknight", 200) { msg: String, code: Int ->
"msg:$msg, code:$code"
}(" Return value ", 300)
println("lambdaFunStr:$lambdaFunStr")
}
fun doLogin11(userName: String, userPwd: String, serverResponse: (String, Int) -> String) {
if (DB_SAVE_USER_NAME == userName && DB_SAVE_USER_PWD == userPwd) {
val serverResponseResult = " Congratulations , Login successful !"
serverResponse(serverResponseResult, 200)
} else {
val serverResponseResult = " I'm sorry , Login failed !"
serverResponse(serverResponseResult, 404)
}
}
fun lambdaFun(str1: String, int1: Int, lambdaAction: (String, Int) -> String): (String, Int) -> String {
lambdaAction(str1, int1)
return { str1, int1 -> "str1:$str1, int1:$int1" }
}
边栏推荐
- 解析机器人科技发展观对社会研究论
- [quick start of Digital IC Verification] 14. Basic syntax of SystemVerilog learning 1 (array, queue, structure, enumeration, string... Including practical exercises)
- Open3D ISS关键点
- PVTV2--Pyramid Vision TransformerV2学习笔记
- Blob object introduction
- Call pytorch API to complete linear regression
- Splunk query CSV lookup table data dynamic query
- Qinglong panel -- finishing usable scripts
- Tuowei information uses the cloud native landing practice of rainbow
- Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience
猜你喜欢

opencv学习笔记二——图像基本操作

Analyzing the influence of robot science and technology development concept on Social Research

The truth of robot education in hands-on practice

机器人教育在动手实践中的真理

The field value in Splunk subquery fuzzy matching CSV is*

Give full play to the wide practicality of maker education space

PVTV2--Pyramid Vision TransformerV2学习笔记

Famine cloud service management script

JS copy picture to clipboard read clipboard

Golang 编译约束/条件编译 ( // +build <tags> )
随机推荐
[IELTS speaking] Anna's oral learning records part2
漏洞复现-Fastjson 反序列化
Coquette data completes the cloud native transformation through rainbow to realize offline continuous delivery to customers
Use of out covariance and in inversion in kotlin
Transformation function map and flatmap in kotlin
Complex network modeling (II)
Leetcode 187 Repeated DNA sequence (2022.07.06)
Xcit learning notes
Infix keyword infix expression and the use of generic extension function in kotlin
[quick start of Digital IC Verification] 10. Verilog RTL design must know FIFO
[untitled]
opencv学习笔记一——读取图像的几种方法
Qinglong panel - today's headlines
Using nocalhost to develop microservice application on rainbow
It's too true. There's a reason why I haven't been rich
Easy to understand SSO
Rainbond 5.6 版本发布,增加多种安装方式,优化拓扑图操作体验
在Rainbond中一键部署高可用 EMQX 集群
OpenVSCode云端IDE加入Rainbond一体化开发体系
Quick analysis of Intranet penetration helps the foreign trade management industry cope with a variety of challenges