当前位置:网站首页>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" }
}
边栏推荐
- Lua programming learning notes
- Rainbond 5.7.1 支持对接多家公有云和集群异常报警
- Wang Zijian: is the NFT of Tencent magic core worth buying?
- opencv学习笔记三——图像平滑/去噪处理
- Infix keyword infix expression and the use of generic extension function in kotlin
- The field value in Splunk subquery fuzzy matching CSV is*
- Splunk中single value视图使用将数值替换为文字
- Give full play to the wide practicality of maker education space
- [step on the pit series] H5 cross domain problem of uniapp
- The simple problem of leetcode is to judge whether the number count of a number is equal to the value of the number
猜你喜欢

Using nocalhost to develop microservice application on rainbow

CCTV is so warm-hearted that it teaches you to write HR's favorite resume hand in hand

在Rainbond中实现数据库结构自动化升级

在Rainbond中一键部署高可用 EMQX 集群

打通法律服务群众“最后一公里”,方正璞华劳动人事法律自助咨询服务平台频获“点赞”

OpenVSCode云端IDE加入Rainbond一体化开发体系
![[step on the pit series] H5 cross domain problem of uniapp](/img/53/bd836a5c5545f51be929d8d123b961.png)
[step on the pit series] H5 cross domain problem of uniapp

The single value view in Splunk uses to replace numeric values with text

在 Rainbond 中一键安装高可用 Nacos 集群

SSM 整合
随机推荐
Splunk查询csv lookup table数据动态查询
Rainbond结合NeuVector实践容器安全管理
Deit learning notes
Open3d ISS key points
Ebpf cilium practice (2) - underlying network observability
Lua 编程学习笔记
BiSeNet的特點
Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
opencv学习笔记一——读取图像的几种方法
It's too true. There's a reason why I haven't been rich
Rainbond 5.6 版本发布,增加多种安装方式,优化拓扑图操作体验
Understanding of out covariance, in inversion and invariance in kotlin
Wang Zijian: is the NFT of Tencent magic core worth buying?
The field value in Splunk subquery fuzzy matching CSV is*
Rainbow combines neuvector to practice container safety management
IELTS review progress and method use [daily revision]
Transformation function map and flatmap in kotlin
rsync远程同步
Vulnerability recurrence easy_ tornado
单元测试报告成功率低