简体   繁体   English

kotlin 中的多个高阶函数

[英]More than one Higher Order Functions in kotlin

Can we pass two higher order functions or more than one, in function's parameter?我们可以在函数的参数中传递两个或多个高阶函数吗?
If so, then how we going to call that function which contain those two or more than one higher order functions as a parameter...如果是这样,那么我们将如何调用包含这两个或多个高阶函数作为参数的函数......

Of couse you can.你当然可以。 You can have any number of lambda parameters just like for parameters of any other type.您可以拥有任意数量的 lambda 参数,就像任何其他类型的参数一样。

Even Kotlins standard library makes use of that.甚至 Kotlins 标准库也使用了它。 For example the generateSequence function.例如generateSequence函数。

generateSequence(seedFunction = { 1 }, nextFunction = { it + 1 })

The only difference to a non-lambda parameter is that you can omit the parentheses if the last parameter of your function is a lambda.与非 lambda 参数的唯一区别是,如果函数的最后一个参数是 lambda,则可以省略括号。 So you could call generateSequence like this too:所以你也可以像这样调用generateSequence

generateSequence(seedFunction = { 1 }) { it + 1 }

Wording措辞

There might be a misunderstanding of what a Higher-Order function is:可能对高阶函数是什么有误解:
GeeksForGeeks :GeeksForGeeks :

In Kotlin, a function which can accepts a function as parameter or can returns a function is called Higher-Order function.在 Kotlin 中,可以接受函数作为参数或可以返回函数的函数称为高阶函数。

And now the definition for functions:现在是函数的定义:

Kotlinlang: 科特林朗:

Kotlin functions are first-class, which means that they can be stored in variables and data structures, passed as arguments to and returned from other higher-order functions. Kotlin 函数是一流的,这意味着它们可以存储在变量和数据结构中,作为参数传递给其他高阶函数并从其他高阶函数返回。 You can operate with functions in any way that is possible for other non-function values.您可以以任何可能用于其他非函数值的方式对函数进行操作。

Conclusion结论

Functions can be passed as parameters into methods - just as many as you like.函数可以作为参数传递给方法——只要你喜欢就可以。 High-order is just a (descriptive) type, which means that your method either takes functions as parameters, or returns a function.高阶只是一种(描述性)类型,这意味着您的方法要么将函数作为参数,要么返回一个函数。

Example:例子:

fun <T> takeFiveFunctions(
    block1 : () -> Unit,
    block2 : (T) -> Unit,
    block3 : () -> T,
    block4 : (T) -> T,
    block5 : (List<T>) -> T
) : Boolean = true

fun main() {
    takeFiveFunctions<Int>(
        block1 = { /*do something 1*/ },
        block2 = { print(it) },
        block3 = { 2 },
        block4 = { it * 3 },
        block5 = { it.first() }
    )
}

EDIT编辑

but some other syntax says we can take out lambda from parentheses但其他一些语法说我们可以从括号中取出 lambda

Kotlinlang says : Kotlinlang 说

In Kotlin, there is a convention: if the last parameter of a function is a function, then a lambda expression passed as the corresponding argument can be placed outside the parentheses:在 Kotlin 中,有一个约定:如果函数的最后一个参数是函数,那么作为相应参数传递的 lambda 表达式可以放在括号外:

Using my previous example, it would look like this:使用我之前的示例,它看起来像这样:

fun main() {
    takeFiveFunctions<Int>(
        block1 = { /*do something 1*/ },
        block2 = { print(it) },
        block3 = { 2 },
        block4 = { it * 3 }
    ) { it.first()}
}

Yes, why not.是的,为什么不。 You can just do that:你可以这样做:

fun <T1, T2, T3> Collection<T1>.higherOrder(
  firstFun: (T1)->T2,
  secondFun: (T2)->T3)
: List<T3> {
  return this.map(firstFun).map(secondFun)
}

Invocation:调用:

val result = listOf(3.14, 6.9, 42.0).higherOrder({ it.toInt() }, { it.toString() })
// ["3", "6", "42"]

Kotlin allows to move last lambda parameter outside parameter list, so you can also write: Kotlin 允许将最后一个 lambda 参数移到参数列表之外,因此您也可以编写:

val result = listOf(3.14, 6.9, 42.0).higherOrder({ it.toInt() }) { it.toString() }

But in case of multiple functional parameters, that's bad practice.但在多个功能参数的情况下,这是不好的做法。 The best you can do is to specify parameter names explicitly:您能做的最好的事情是明确指定参数名称:

val result = listOf(3.14, 6.9, 42.0).higherOrder(
  firstFun = { it.toInt() }, 
  secondFun = { it.toString() }
)

Which is much more readable.哪个更具可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM