当前位置:网站首页>Thirteen forms of lambda in kotlin
Thirteen forms of lambda in kotlin
2022-07-07 08:24:00 【yu-Knight】
//Kotlin in Lambda Thirteen forms of
fun main(args: Array<String>) {
// Declaration of functions
var method1: () -> Unit
var method2: (Int, Int) -> Int
var method3: (String, Boolean) -> Any?
var method4: (Int, Double, Long, String?) -> Boolean
// method4(1, 34.4, 345, null)
// Implementation of function
// Functional Statement + Functional Realization
// :( Parameters ) -> return Mode one ()
// = { Parameters -> return } Mode two {}
// Mode one
// This is a function , Just this function , There are no input and output parameters
val method5 = { println("yuknight") }
method5() // call method5 function
method5.invoke() //method5 Function call itself
// Mode two
// This is a function , Just this function , No input parameters , There are output parameters String type
val method6 = { "yuknight" }
println(method6())
println(method6.invoke())
// Mode three
// This is a function , Just this function , Input parameter is 2 individual Int type , Output parameters String type
val method7 = { number1: Int, number2: Int -> number1.toString() + number2.toString() }
println(method7(5, 2))
// Mode 4 (Int, Int)->Int
var method8 = { number1: Int, number2: Int -> number1 + number2 }
// (Int, Int) -> Int Omit not to write , Automatically infer types
//var method8: (Int, Int) -> Int = { number1: Int, number2: Int -> number1 + number2 }
// Methods five
var method9: (Int) -> String // Declaration of functions
method9 = fun(value: Int): String = value.toString() + "yuknight" // Implementation of function
println(method9.invoke(22))
// Mode 6 Statement + Realization
var method10: (Int, Int) -> String = fun(number1, number2) = number1.toString() + number2.toString()
// println(method10.invoke(2,3))
println(method10(2, 3))
if (method10(2, 3) is String) println(" The output is String") else println(" Output is not String")
// Mode 7
var method11: (String, String) -> Unit = { aStr, bStr -> println("aStr:$aStr, bStr:$bStr") }
method11("122", "333")
method11.invoke("122", "333")
// Mode 8
var method12: (String) -> Int = {
println(" What's coming in is :$it")
34 // The last line returns the value
}
var r = method12.invoke("yuknght")
println(r)
// Mode 9
var method13: (Int) -> Unit = {
when (it) {
1 -> println(" The value passed in is 1")
in 20..60 -> println(" The value passed in is 20 To 60 Number between ")
else -> println(" Are not satisfied ")
}
}
method13.invoke(30)
// Mode 10
var method14: (Int, Int) -> Unit = { _, number2 ->
println(" The second parameter is : $number2")
}
method14(3, 90)
// Mode 11
var method15 = { str: Any -> str }
println(method15("yuknight"))
println(method15(23))
println(method15(false))
// Method 12
var method16 = { sex: Char ->
if (sex == ' male ') println(" The representative is Mr ")
else if (sex == ' Woman ') println(" It means girls ")
else println(" Unknown humans ")
}
method16.invoke(' male ')
// Method 13
var method17:String.()->Unit ={
println(" Call object :$this")
}
"yuknight".method17() // Yes String Type object
}边栏推荐
- Pytoch (VI) -- model tuning tricks
- Caractéristiques de bisenet
- Leetcode 187 Repeated DNA sequence (2022.07.06)
- 漏洞复现-Fastjson 反序列化
- 发挥创客教育空间的广泛实用性
- Pvtv2--pyramid vision transformer V2 learning notes
- Using helm to install rainbow in various kubernetes
- Vulnerability recurrence fastjson deserialization
- SSM 整合
- Qinglong panel - today's headlines
猜你喜欢

The largest 3 same digits in the string of leetcode simple question

Uniapp mobile terminal forced update function

Rainbond 5.7.1 支持对接多家公有云和集群异常报警

Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience

One click deployment of highly available emqx clusters in rainbow

Splunk子查询模糊匹配csv中字段值为*

CDC (change data capture technology), a powerful tool for real-time database synchronization
![[quick start of Digital IC Verification] 11. Introduction to Verilog testbench (VTB)](/img/c2/32a2c1ede493b778a6c44077d765d0.png)
[quick start of Digital IC Verification] 11. Introduction to Verilog testbench (VTB)

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

Rainbow combines neuvector to practice container safety management
随机推荐
[quick start of Digital IC Verification] 11. Introduction to Verilog testbench (VTB)
[quick start of Digital IC Verification] 10. Verilog RTL design must know FIFO
【雅思口语】安娜口语学习记录 Part3
数据中台落地实施之法
[quick start of Digital IC Verification] 14. Basic syntax of SystemVerilog learning 1 (array, queue, structure, enumeration, string... Including practical exercises)
eBPF Cilium实战(2) - 底层网络可观测性
Splunk子查询模糊匹配csv中字段值为*
One click deployment of highly available emqx clusters in rainbow
Leetcode 187 Repeated DNA sequence (2022.07.06)
使用 Nocalhost 开发 Rainbond 上的微服务应用
[quick start of Digital IC Verification] 13. SystemVerilog interface and program learning
Interview questions (CAS)
One click installation of highly available Nacos clusters in rainbow
【无标题】
opencv学习笔记五——梯度计算/边缘检测
Splunk中single value视图使用将数值替换为文字
归并排序和非比较排序
IELTS review progress and method use [daily revision]
Qinglong panel - today's headlines
Quick analysis of Intranet penetration helps the foreign trade management industry cope with a variety of challenges