当前位置: 首页 > backend >正文

Kotlin 高阶函数初步学习

### 高阶函数f(g(x))
* 并不复杂,只是把函数作为参数或者返回值的一类函数而已,
* 是Kotlin函数式编程特性的核心,允许将函数作为参数传递或返回,极大提升代码的灵活性和可复用性

```
fun main9(args: Array<String>) {
val numbers = listOf(8,10)
args.forEach(::println)
//三种函数引用的方式
//高阶函数
val helloWorld = Hello::world
//直接用类名引用,相当于lamat表达式传入string
args.filter(String::isNotEmpty)

    val pdfPrinter = PdfPrinter()
numbers.forEach(pdfPrinter::println2)
val newNam = numbers.map {
it * 2 + 3
}.distinct().forEach(pdfPrinter::println)
processList(numbers) { num -> println(num) }
}

class PdfPrinter {
fun println(any: Any) {
kotlin.io.println(any)
}

    fun println2(any: Any) {
kotlin.io.println("any $any")
}
}

class Hello {
fun world() {
println("Hello World.")
}
}
```

#### 集合操作高阶函数
* map‌:转换集合元素,返回新集合。
* filter‌:筛选符合条件的元素。
* reduce‌:将集合合并为单一结果(如累加)。
* flatMap‌:扁平化嵌套集合

```
val numbers = listOf(1, 2, 3)
val even = listOf(1, 2, 3).map(Int::toDouble) //[1.0, 2.0, 3.0]
val evens = numbers.filter { it % 2 == 0 }  //[2]
val sum = numbers.reduce { acc, num -> acc + num } //6
val nested = listOf(listOf(1, 2), listOf(3)) // [[1, 2], [3]]
val flat = nested.flatMap { it } // [1, 2, 3]
listOf("abc","def").flatMap { it.toList() } // 输出 [a,b,c,d,e,f]
nested.flatten() // [1, 2, 3]
val list0 = listOf(1..5, 2..4)
val list = listOf(1..3, 2..4, 3)
//如果是list,这里就会报错,flatMap 要求传入的 lambda 必须返回 Iterable,但 rangeInt 可能为 Int 等基础类型,无法直接调用
val flatList0 = list0.flatMap { rangeInt ->
rangeInt.map { element ->
"ddd $element"
}
} // [ddd 1, ddd 2, ddd 3, ddd 4, ddd 5, ddd 2, ddd 3, ddd 4]
val flatList = list.flatMap { rangeInt ->
when (rangeInt) {
is Iterable<*> -> rangeInt.map { "ddd $it" }
else -> listOf("sss $rangeInt") // 处理非迭代元素
}
}// [ddd 1, ddd 2, ddd 3, ddd 2, ddd 3, ddd 4, sss 3]

    println("$list0 list $list") //[1..5, 2..4] list [1..3, 2..4, 3]
```

```
println((0..6).map(::factorila).fold(5) { acc, i ->
acc + i
}) //62
println((numbers).joinToString { "S" })//S, S, S, S, S, S, S
println((0..3).joinToString("dj"))//0dj1dj2dj3
println((0..6).map(::factorila))//[1, 1, 3, 6, 10, 15, 21]
println((0..6).map(::factorila).filter { it % 2 == 1 })//[1, 1, 3, 15, 21]
println((0..6).map(::factorila).filterIndexed { index, i -> i % 2 == 1 })//[1, 1, 3, 15, 21]
println((0..6).map(::factorila).filterIndexed { index, i -> index % 2 == 1 })//[1, 6, 15]
println((0..6).map(::factorila).takeWhile { it % 2 == 1 })//[1, 1, 3]
println((0..6).map(::factorila).takeLastWhile { it % 2 == 1 })//[15, 21]
println((0..6).map(::factorila).fold(StringBuilder()) { acc, i ->
acc.append(i).append(",")
})//1,1,3,6,10,15,21,
println((0..6).map(::factorila).foldRight(StringBuilder()) { i, acc ->
acc.append(i).append(",")
})//21,15,10,6,3,1,1,
```
```
fun factorila(n: Int): Int {
if (n == 0) return 1
return (1..n).reduce { acc, num -> acc + num }
}
```
#### 作用域函数
*  let‌:对非空对象执行操作,返回 Lambda 结果。
*  apply‌:对象初始化后立即进行配置‌对象属性,后返回自身。在apply里面相当于在person里面调用person的成员;适合 ‌对象初始化或链式配置‌
*  run‌:结合对象调用与返回结果。返回 Lambda 最后一行结果‌
*  also‌:执行副作用操作(如日志记录)后,返回原对象

```
println("Kotlin".let { it.length }) // 6
println(mutableListOf<Int>().apply { add(1); add(2) })// [1,2]
println("Hello".run { length }) //5
numbers.also(::println)//[1, 2, 3]
println(numbers.also { num ->
println("处理前: $num") //处理前: [1, 2, 3]
println(num.map(::factorila).filterIndexed { index, i -> i % 2 == 1 })//[1, 3]
})//[1, 2, 3]

    println(findPerson()?.apply {
work2()
age++
println(age)//19
})//Person2(name=GD, age=19)
```


*  with:对同一个对象执行多个操作,无需重复引用对象名‌,返回 lambda 表达式的最后一行结果‌
*  use : 自动管理实现了 Closeable 的资源‌,返回 lambda 表达式结果‌多用于文件/网络流等需要关闭的资源操作
```
var br = BufferedReader(FileReader("hello.txt"))
with(br) {
var line: String?
while (true) {
line = readLine() ?: break
println(line)
}
close()
}

BufferedReader(FileReader("hello.txt")).use {//use自带close
var line: String?
while (true) {
line = it.readLine() ?: break
println(line)
}
}
```

http://www.xdnf.cn/news/15909.html

相关文章:

  • Laravel 系统版本查看及artisan管理员密码找回方法针对各个版本通用方法及原理-优雅草卓伊凡
  • 信息学奥赛一本通 1576:【例 2】选课 | 洛谷 P2014 [CTSC1997] 选课
  • 子网划分核心原理 (网络原理1)
  • [学习] Hilbert变换:从数学原理到物理意义的深度解析与仿真实验(完整实验代码)
  • 《通信原理》学习笔记——第五章
  • Spring 源码阅读(二) 核心概念解析 ApplicationContext、类型转化
  • 【PyTorch】图像二分类项目
  • 【JS逆向基础】数据库之mysql
  • 7.19-7.20 Java基础 | File类 I/O流学习笔记
  • pyhton基础【27】课后拓展
  • 【Linux】3. Shell语言
  • 深度相机的工作模式(以奥比中光深度相机为例)
  • SQL Server(2022)安装教程及使用_sqlserver下载安装图文
  • 《计算机网络》实验报告四 TCP协议分析
  • 0719代码调试记录
  • IsaacLab学习记录(四)
  • Milvus Dify 学习笔记
  • 题单【循环结构】
  • 基于单片机出租车计价器设计
  • 30天打牢数模基础-决策树讲解
  • 【C语言】字符串与字符函数详解(上)
  • C++ 并发 future, promise和async
  • 数位 dp
  • 「Java案例」利用方法打印乘法表
  • WPF学习笔记(28)Interaction.Triggers的意义与使用方式
  • dify创建OCR工作流
  • NX584NX559美光固态闪存NX561NW993
  • AI(学习笔记第六课) 使用langchain进行AI开发 load documents(csv和文件夹)
  • 开源社区贡献指南:如何通过Three.js插件开发提升企业技术影响力?
  • Windows批量修改文件属性方法