FSA全栈行动 FSA全栈行动
首页
  • 移动端文章

    • Android
    • iOS
    • Flutter
  • 学习笔记

    • 《Kotlin快速入门进阶》笔记
    • 《Flutter从入门到实战》笔记
    • 《Flutter复习》笔记
前端
后端
  • 学习笔记

    • 《深入浅出设计模式Java版》笔记
  • 逆向
  • 分类
  • 标签
  • 归档
  • LinXunFeng
  • GitLqr

公众号:FSA全栈行动

记录学习过程中的知识
首页
  • 移动端文章

    • Android
    • iOS
    • Flutter
  • 学习笔记

    • 《Kotlin快速入门进阶》笔记
    • 《Flutter从入门到实战》笔记
    • 《Flutter复习》笔记
前端
后端
  • 学习笔记

    • 《深入浅出设计模式Java版》笔记
  • 逆向
  • 分类
  • 标签
  • 归档
  • LinXunFeng
  • GitLqr
  • Kotlin - 数据类型
  • Kotlin - 类与构造器
  • Kotlin - 空类型和智能类型转换
  • Kotlin - 区间与数组
  • Kotlin - 常量与变量
  • Kotlin - 函数与Lambda表达式
  • Kotlin - 类成员
  • Kotlin - 运算符与中缀表达式
  • Kotlin - 分支与循环
  • Kotlin - 参数与异常
  • Kotlin - 面向对象之抽象类与接口
  • Kotlin - 面向对象之继承与实现
  • Kotlin - 类及成员的可见性
  • Kotlin - 伴生对象与静态成员
  • Kotlin - 方法重载与默认参数
  • Kotlin - 扩展成员
  • Kotlin - 属性代理
  • Kotlin - 数据类
  • Kotlin - 内部类
  • Kotlin - 枚举与密封类
  • Kotlin - 高阶函数与函数引用
  • Kotlin - 常见高阶函数
    • forEach
    • map
    • flatMap
    • filter
    • takeWhile
    • reduce
    • fold
  • Kotlin - 作用域函数
  • Kotlin - 尾递归优化
  • Kotlin - 函数式编程
  • Kotlin - 改良设计模式 - 工厂模式
  • Kotlin - 改良设计模式 - 构建者模式
  • Kotlin - 改良设计模式 - 观察者模式
  • Kotlin - 改良设计模式 - 策略模式
  • Kotlin - 改良设计模式 - 迭代器模式
  • Kotlin - 改良设计模式 - 责任链模式
  • Kotlin - 改良设计模式 - 装饰者模式
  • 《Kotlin快速入门进阶》
GitLqr
2021-02-02
目录

Kotlin - 常见高阶函数

欢迎关注微信公众号:[FSA全栈行动 👋]

# forEach

高阶函数 forEach 是可迭代对象的扩展方法,接收函数类型是 (T) -> Unit 的参数 action,forEach 会将 action 这个函数作用于可迭代对象中的每个元素,这是源码:

/**
 * Performs the given [action] on each element.
 */
@kotlin.internal.HidesMembers
public inline fun <T> Iterable<T>.forEach(action: (T) -> Unit): Unit {
    for (element in this) action(element)
}

根据 forEach 的入参要求,我们给其传递一个 lambda 表达式或是函数引用:

fun main(args: Array<String>) {
    val list = listOf(1, 2, 3, 4, 5, 6, 7, 8)
    // list.forEach { it -> println(it) } // it可以省略
    list.forEach { println(it) }
    list.forEach(::println)
    list.forEachIndexed { index, i -> println("index=$index, value=$i") }
}

forEachIndexed 相比 forEach 只是多了索引 index。

# map

高阶函数 map 也是可迭代对象的扩展方法,根据 map 的源码与注释,我们知道 map 接收一个类型是 (T) -> R 的参数 transform,map 会将 transform 作用于可迭代对象中的每个元素,并最终返回一个新的集合 List:

/**
 * Returns a list containing the results of applying the given [transform] function
 * to each element in the original collection.
 *
 * @sample samples.collections.Collections.Transformations.map
 */
public inline fun <T, R> Iterable<T>.map(transform: (T) -> R): List<R> {
    return mapTo(ArrayList<R>(collectionSizeOrDefault(10)), transform)
}

借助 map 的功能,我们可以将一个数组  “映射” 成另一个数组,这在日常开发很有用:

fun main(args: Array<String>) {
    val list = listOf(1, 2, 3, 4, 5, 6, 7, 8)

    // 将int数组中的元素经过某种运算形成一个新的int数组
    val newList = list.map { it * 2 + 3 }
    newList.forEach(::println)

    // 将int转成double
    val newList2 = list.map(Int::toDouble) // (Int) -> Double
    newList2.forEach(::println)
}

注意:toDouble()是 Int 类中的一个方法,在函数引用部分已经讲过,当使用 类名::方法名 这种方式引用一个成员方法时,会自动在函数类型的参数列表第 1 位多出一个接收者 Receiver,用于接收类实例对象 ,刚好 toDouble()没有参数列表,因此 Int::toDouble 对应的函数类型是 (Int) -> Double,符合高阶函数 map 的参数要求。

# flatMap

高阶函数 flatMap 比 map 高一个维度,可以将可迭代对象中的每个可迭代对象进行处理,最终返回一个 扁平化 的可迭代对象,注意参数 transform 的函数类型是 (T) -> Iterable<R>:

/**
 * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original collection.
 *
 * @sample samples.collections.Collections.Transformations.flatMap
 */
public inline fun <T, R> Iterable<T>.flatMap(transform: (T) -> Iterable<R>): List<R> {
    return flatMapTo(ArrayList<R>(), transform)
}

什么是 扁平化 ,直观的说,就是胖变瘦,多维变一维:

fun main(args: Array<String>) {
    val list = listOf(
        8..10,
        1..3,
        98..100
    )
    val flatList = list.flatMap { it } // it->it,这里同时也是 (Iterable) -> Iterable
    flatList.forEach(::println) // 这时的 flatList 就相当于 [8,9,10,1,2,3,98,99,100]
}

flatMap 除了 扁平化 这个特性外,也拥有 map 的特性,可以将可迭代对象中的元素进行转换处理:

fun main(args: Array<String>) {
    val list = listOf(
        8..10,
        1..3,
        98..100
    )
    val flatList2 = list.flatMap { intRange ->
        intRange.map { intElement -> "No. $intElement " } // 把Int转成String
    }
    flatList2.forEach(::print) // No. 8 No. 9 No. 10 No. 1 No. 2 No. 3 No. 98 No. 99 No. 100
}

# filter

高阶函数 filter 可以将可迭代对象进行过滤,只有满足 predicate 过滤条件的元素(即 return true)才会被 "留下":

/**
 * Returns a list containing only elements matching the given [predicate].
 *
 * @sample samples.collections.Collections.Filtering.filter
 */
public inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean): List<T> {
    return filterTo(ArrayList<T>(), predicate)
}

我们可以使用 filter 过滤出数组中的奇数:

fun main(args: Array<String>) {
    val list = listOf(1, 2, 3, 4, 5, 6, 7, 8)
    val newList = list.filter { it % 2 == 1 }
    newList.forEachIndexed { index, i -> print(if (index > 0) " $i" else i) } // 1 3 5 7
}

# takeWhile

高阶函数 takeWhile 会在遇到第一个不符合条件的元素时就结束取数据,留下前面的作为新的集合返回:

/**
 * Returns a list containing first elements satisfying the given [predicate].
 *
 * @sample samples.collections.Collections.Transformations.take
 */
public inline fun <T> Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T> {
    val list = ArrayList<T>()
    for (item in this) {
        if (!predicate(item))
            break
        list.add(item)
    }
    return list
}

我们可以使用 takeWhile 筛选出前面满足条件的元素:

fun main(args: Array<String>) {
    val list = listOf(1, 1, 2, 3, 5, 8, 13, 21)

    var newList = list.takeWhile { it % 2 == 1 } // 筛选出前面的奇数
    newList.forEachIndexed { index, i -> print(if (index > 0) " $i" else i) } // 1 1

    newList = list.takeWhile { it < 5 } // 筛选出前面小于5的数
    newList.forEachIndexed { index, i -> print(if (index > 0) " $i" else i) } // 1 1 2 3
}

# reduce

高阶函数 reduce 会从第一个元素开始累加,并从左到右将 operation 函数应用于当前累加值和每个元素:

/**
 * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element.
 *
 * @sample samples.collections.Collections.Aggregates.reduce
 */
public inline fun <S, T : S> Iterable<T>.reduce(operation: (acc: S, T) -> S): S {
    val iterator = this.iterator()
    if (!iterator.hasNext()) throw UnsupportedOperationException("Empty collection can't be reduced.")
    var accumulator: S = iterator.next()
    while (iterator.hasNext()) {
        accumulator = operation(accumulator, iterator.next())
    }
    return accumulator
}

我们可以用 reduce 来处理一些累加的操作,如计算 1 到 8 之间所有的数进行求和:

fun main(args: Array<String>) {
    val list = listOf(1, 2, 3, 4, 5, 6, 7, 8)
    val result = list.reduce { acc, i -> acc + i }
    println(result) // 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36
}

# fold

高阶函数 fold 跟 reduce 差不多,只是 fold 多了一个初始值,后续处理与 reduce 一样:

/**
 * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element.
 */
public inline fun <T, R> Iterable<T>.fold(initial: R, operation: (acc: R, T) -> R): R {
    var accumulator = initial
    for (element in this) accumulator = operation(accumulator, element)
    return accumulator
}

我们可以 fold 来计算在 100 的基础上,再累加 1 到 8 的和:

fun main(args: Array<String>) {
    val list = listOf(1, 2, 3, 4, 5, 6, 7, 8)
    val result = list.fold(100, { acc, i -> acc + i })
    println(result) // 100 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 37
}
#forEach#map#flatMap#filter#reduce#fold#takeWhile
Kotlin - 高阶函数与函数引用
Kotlin - 作用域函数

← Kotlin - 高阶函数与函数引用 Kotlin - 作用域函数→

最近更新
01
Flutter - Xcode16 还原编译速度
04-05
02
AI - 免费的 Cursor 平替方案
03-30
03
Android - 2025年安卓真的闭源了吗
03-28
更多文章>
Theme by Vdoing | Copyright © 2020-2025 FSA全栈行动
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×