当前位置:网站首页>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" }
}
边栏推荐
- MES system is a necessary choice for enterprise production
- Basic use of CTF web shrink template injection nmap
- One click installation of highly available Nacos clusters in rainbow
- 提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
- Splunk查询csv lookup table数据动态查询
- Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms
- Snyk dependency security vulnerability scanning tool
- Bayes' law
- Qinglong panel -- Huahua reading
- 一种适用于应用频繁测试下快速查看Pod的日志的方法(grep awk xargs kuberctl)
猜你喜欢
opencv学习笔记五——梯度计算/边缘检测
Game attack and defense world reverse
CCTV is so warm-hearted that it teaches you to write HR's favorite resume hand in hand
使用BiSeNet实现自己的数据集
Caractéristiques de bisenet
Openvscode cloud ide joins rainbow integrated development system
XCiT学习笔记
云原生存储解决方案Rook-Ceph与Rainbond结合的实践
在Rainbond中实现数据库结构自动化升级
Vulnerability recurrence easy_ tornado
随机推荐
The reified keyword in kotlin is used for generics
Don't stop chasing the wind and the moon. Spring mountain is at the end of Pingwu
Learn how to compile basic components of rainbow from the source code
探索STEAM艺术设计中的创造力
Splunk中single value视图使用将数值替换为文字
ZCMU--1396: 队列问题(2)
Use of out covariance and in inversion in kotlin
Easy to understand SSO
Domain specific language / DSL in kotlin
Myabtis_ Plus
One click deployment of highly available emqx clusters in rainbow
Opencv learning notes II - basic image operations
The largest 3 same digits in the string of leetcode simple question
Splunk query CSV lookup table data dynamic query
The simple problem of leetcode is to judge whether the number count of a number is equal to the value of the number
使用SwinUnet训练自己的数据集
opencv学习笔记五——梯度计算/边缘检测
[IELTS speaking] Anna's oral learning records part2
Open3D ISS关键点
Call pytorch API to complete linear regression