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)

}

}

```

相关推荐
cpsvps_net23 分钟前
美国服务器环境下Windows容器工作负载智能弹性伸缩
windows
甄超锋1 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
cpsvps3 小时前
美国服务器环境下Windows容器工作负载基于指标的自动扩缩
windows
网硕互联的小客服6 小时前
Apache 如何支持SHTML(SSI)的配置方法
运维·服务器·网络·windows·php
etcix6 小时前
implement copy file content to clipboard on Windows
windows·stm32·单片机
许泽宇的技术分享7 小时前
Windows MCP.Net:基于.NET的Windows桌面自动化MCP服务器深度解析
windows·自动化·.net
非凡ghost8 小时前
AMS PhotoMaster:全方位提升你的照片编辑体验
windows·学习·信息可视化·软件需求
mortimer9 小时前
一次与“顽固”外部程序的艰难交锋:subprocess 调用exe踩坑实录
windows·python·ai编程
gameatp12 小时前
从 Windows 到 Linux 服务器的全自动部署教程(免密登录 + 压缩 + 上传 + 启动)
linux·服务器·windows
穷人小水滴12 小时前
在 windows 运行 flatpak 应用 (WSL)
linux·windows·ubuntu