zl程序教程

您现在的位置是:首页 >  后端

当前栏目

《Kotlin极简教程》第六章 Kotlin函数式编程(FP)

Kotlin教程编程 函数 第六章 极简 fp
2023-09-14 09:07:23 时间

Kotlin对函数式编程的实现恰到好处。

正式上架:《Kotlin极简教程》Official on shelves: Kotlin Programming minimalist tutorial
京东JD:https://item.jd.com/12181725.html
天猫Tmall:https://detail.tmall.com/item.htm?id=558540170670

函数指针

/**
 * "Callable References" or "Feature Literals", i.e. an ability to pass
 * named functions or properties as values. Users often ask
 * "I have a foo() function, how do I pass it as an argument?".
 * The answer is: "you prefix it with a `::`".
 */

fun main(args: Array<String>) {
    val numbers = listOf(1, 2, 3)
    println(numbers.filter(::isOdd))
}

fun isOdd(x: Int) =