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)

}

}

```

相关推荐
安心落意2 小时前
Windows Server2022下使用SQL Server2019开发版搭建高可用集群
网络·windows·数据挖掘·数据库高可用·aways on
Maybe_ch3 小时前
WinForm-免费,可商用的WinForm UI框架推荐
windows·ui·c#
小瞿慢慢跑5 小时前
合并pdf工具下载
windows·pdf
__风__6 小时前
PostgreSQL SysCache & RelCache
数据库·windows·postgresql
chilavert31810 小时前
技术演进中的开发沉思-41 MFC系列:定制 AppWizard
数据库·c++·windows·mfc
深盾科技10 小时前
从基础到实战:.NET 反射机制的进阶用法与最佳实践
windows·microsoft·.net
-曾牛12 小时前
在Windows Server 2012 R2中安装与配置IIS服务并部署mssql靶机教程
运维·服务器·windows·网络安全·sqlserver·渗透测试·渗透测试靶机
.m14 小时前
讯飞输入法3.0.1742功能简介
windows
救救孩子把16 小时前
在 Windows上用WSL和VSCode进行Linux开发环境配置
linux·windows·vscode·wsl