当前位置:网站首页>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" }
}
边栏推荐
- Le système mes est un choix nécessaire pour la production de l'entreprise
- Rainbond 5.7.1 支持对接多家公有云和集群异常报警
- Complex network modeling (II)
- Low success rate of unit test report
- ZCMU--1396: 队列问题(2)
- Open3d ISS key points
- Excel import function of jeesite form page
- 提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
- Using helm to install rainbow in various kubernetes
- 云原生存储解决方案Rook-Ceph与Rainbond结合的实践
猜你喜欢
The largest 3 same digits in the string of leetcode simple question
Opencv learning notes II - basic image operations
GFS distributed file system
One click installation of highly available Nacos clusters in rainbow
CDC (change data capture technology), a powerful tool for real-time database synchronization
Myabtis_ Plus
Basic use of CTF web shrink template injection nmap
Train your dataset with swinunet
柯基数据通过Rainbond完成云原生改造,实现离线持续交付客户
Analyzing the influence of robot science and technology development concept on Social Research
随机推荐
The use of generics and vararg variable parameters in kotlin
[IELTS speaking] Anna's oral learning records part2
[quick start of Digital IC Verification] 10. Verilog RTL design must know FIFO
Automatic upgrading of database structure in rainbow
使用BiSeNet实现自己的数据集
rsync远程同步
PVTV2--Pyramid Vision TransformerV2学习笔记
Lua programming learning notes
Interview questions (CAS)
Splunk中single value视图使用将数值替换为文字
数据中台落地实施之法
Analyzing the influence of robot science and technology development concept on Social Research
Rsync remote synchronization
利用 Helm 在各类 Kubernetes 中安装 Rainbond
Splunk查询csv lookup table数据动态查询
Battery and motor technology have received great attention, but electric control technology is rarely mentioned?
Open3D ISS关键点
Make LIVELINK's initial pose consistent with that of the mobile capture actor
The simple problem of leetcode is to judge whether the number count of a number is equal to the value of the number
Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience