当前位置:网站首页>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" }
}
边栏推荐
- Zcmu--1396: queue problem (2)
- Excel import function of jeesite form page
- 漏洞复现-Fastjson 反序列化
- Caractéristiques de bisenet
- Full text query classification
- Four items that should be included in the management system of integral mall
- The single value view in Splunk uses to replace numeric values with text
- Golang 编译约束/条件编译 ( // +build <tags> )
- Don't stop chasing the wind and the moon. Spring mountain is at the end of Pingwu
- 使用SwinUnet训练自己的数据集
猜你喜欢
The single value view in Splunk uses to replace numeric values with text
使用 Nocalhost 开发 Rainbond 上的微服务应用
Hisense TV starts the developer mode
Pvtv2--pyramid vision transformer V2 learning notes
解析机器人科技发展观对社会研究论
CTF-WEB shrine模板注入nmap的基本使用
Uniapp mobile terminal forced update function
饥荒云服管理脚本
Obsidan之数学公式的输入
[quick start of Digital IC Verification] 11. Introduction to Verilog testbench (VTB)
随机推荐
探索STEAM艺术设计中的创造力
[untitled]
[IELTS speaking] Anna's oral learning records Part3
Leetcode 187 Repeated DNA sequence (2022.07.06)
Ebpf cilium practice (2) - underlying network observability
【雅思口语】安娜口语学习记录 Part2
Rainbond 5.6 版本发布,增加多种安装方式,优化拓扑图操作体验
Detailed explanation of apply, also, let, run functions and principle analysis of internal source code in kotlin
CDC (change data capture technology), a powerful tool for real-time database synchronization
Avatary's livedriver trial experience
提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
Famine cloud service management script
Improve the delivery efficiency of enterprise products (1) -- one click installation and upgrade of enterprise applications
【无标题】
Open3D ISS关键点
The truth of robot education in hands-on practice
The reified keyword in kotlin is used for generics
JS copy picture to clipboard read clipboard
Wang Zijian: is the NFT of Tencent magic core worth buying?
It's too true. There's a reason why I haven't been rich