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

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.lryc.cn/news/595029.html

相关文章:

  • 【MySQL】Linux配置MySQL Windows远程连接
  • 步进电机基础
  • 基于 Nginx 搭建 OpenLab 多场景 Web 网站:从基础配置到 HTTPS 加密全流程
  • ORA-00600: internal error code, arguments: [krse_arc_source_init.1], [4], [2]
  • 汽车售后诊断仪DoIP和CANBus诊断指令分析
  • Milvus:开源向量数据库的初识
  • HTTP性能优化:打造极速Web体验的关键策略
  • Python 进阶(五): Excel 基本操作
  • Android 版本与 API 级别对照速查表
  • Go语言进阶书籍:Go语言高级编程(第2版)
  • Spring Boot05-热部署
  • Python 高效创建多项式及相关计算方法
  • 基于dcmtk的dicom工具 第八章 echoSCU-dicom测试连接
  • Prompt Engineering(提示词工程)基础了解
  • 第三章自定义检视面板_创建自定义编辑器类_编扩展默认组件的显示面板(本章进度6/9)
  • 14.6 《3步实战LLaMA2-7B指令微调:Hugging Face生态+LoRA技术,MT-Bench得分从5.1直升7.3》
  • LeetCode - 3274. Check if Two Chessboard Squares Have the Same Color
  • 数据结构之克鲁斯卡尔算法
  • C#/.NET/.NET Core技术前沿周刊 | 第 47 期(2025年7.14-7.20)
  • Leetcode力扣解题记录--第238题(前/后缀积)
  • OpenCV学习(二)-二维、三维识别
  • 软件工厂 DevSecOps 场景下的测试体系建设实践
  • Facebook 开源多季节性时间序列数据预测工具:Prophet 乘性季节性 Multiplicative Seasonality
  • 昇腾310P软件安装说明
  • Python----NLP自然语言处理(Doc2Vec)
  • 7-Zip 曝出两个可导致拒绝服务的中危漏洞
  • 【网络安全】DDOS攻击
  • (7)ROS2-MUJOCO联合仿真环境迁移优化
  • 网络协议(三)网络层 IPv4、CIDR(使用子网掩码进行网络划分)、NAT在私网划分中的应用
  • 零基础数据结构与算法——第五章:高级算法-回溯算法N皇后问题