Kotlin高阶函数组成 [英] Kotlin Higher Order Function Composition

查看:156
本文介绍了Kotlin高阶函数组成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何在Kotlin中将一个函数定义为另外两个函数的组合,但是我很挣扎.这是我的代码:

fun compose(a: (Int, Int) -> Int, b: (Int, Int) -> Int): Int {
    return a.invoke() + b.invoke()
}

compose函数的想法是,它将接受两个函数作为输入(两个函数都需要两个Ints并返回一个Int),并返回两个传递的函数的结果之和.问题是我必须调用传递的函数来计算它们的总和(显然是哈哈),但我不知道我想在compose方法内调用的值(它们是传递给函数的值).

我在这里完全错过了什么吗?我知道在Haskell这样的语言中这是可能的,在Kotlin中是否可能?

解决方案

一种方法:

您必须像这样将两个Ints作为附加参数传递给compose:

fun compose(c: Int, d: Int, 
            a: (Int, Int) -> Int, 
            b: (Int, Int) -> Int) = a(c, d) + b(c, d)

Lambda是进一步的抽象层次,它使您有机会使行为可变,但仍然需要向他们提供数据.

更抽象的方法:

您甚至可以进一步抽象,并让compose返回一个lambda,该lambda结合了其他两个lambda的结果(我们称其为compose2):

// return type is inferred to (Int, Int) -> Int
fun compose2(a: (Int, Int) -> Int, b: (Int, Int) -> Int) = { 
     c: Int, d: Int -> a(c, d) + b(c, d)
}

val f = compose2(/* pass lambdas */)

f本身就是一个lambda,可以这样称呼:

f(2, 4)

因此,compose2所做的无非就是返回一个lambda,该lambda将两个传递的lambda的结果相加.实际的调用是在compose2之外进行的.

更抽象的方法:

在撰写功能中,您可以使用简单的加法作为撰写操作.您甚至可以通过传递第三个lambda ab来使此操作变量化,该lambda ab告诉您的函数如何编写ab:

fun compose3(a: (Int, Int) -> Int, 
             b: (Int, Int) -> Int, 
             ab: (Int, Int) -> Int) = { 
    c: Int, d: Int -> ab(a(c, d), b(c, d))
}

结果还是一个lambda,它需要两个Int并返回一个Int.

I'm trying to figure out how I can declaritively define a function as a composition of two other functions in Kotlin but I'm struggling. Here is my code:

fun compose(a: (Int, Int) -> Int, b: (Int, Int) -> Int): Int {
    return a.invoke() + b.invoke()
}

The idea of the compose function is that it will take in two functions as it's input (both of which takes two Ints and return an Int) and return the sum of the results of the two passed functions. The problem is I have to invoke the passed functions to work out their sum (obviously lol) but I don't know the values which I wish to invoke with inside the compose method (they are the values passed to the functions).

Am I completely missing something here? I know this is possible in a language like Haskell, is it possible or not in Kotlin?

解决方案

One way:

You have to pass the two Ints as additional arguments to compose like this:

fun compose(c: Int, d: Int, 
            a: (Int, Int) -> Int, 
            b: (Int, Int) -> Int) = a(c, d) + b(c, d)

Lambdas are a further level of abstraction which give you the opportunity to make behaviour variable, you still have to provide them with data.

A more abstract approach:

You can abstract even further and let compose return a lambda which combines the results of the other two lambdas (lets call it compose2):

// return type is inferred to (Int, Int) -> Int
fun compose2(a: (Int, Int) -> Int, b: (Int, Int) -> Int) = { 
     c: Int, d: Int -> a(c, d) + b(c, d)
}

val f = compose2(/* pass lambdas */)

f is a lambda itself an can be called like this:

f(2, 4)

So, compose2 does nothing more than return a lambda which adds the results of the two passed lambdas. The actual invocation is done outside of compose2.

An even more abstract approach:

In your compose function you use a simple addition as composition operation. You can even make this operation variable, by passing a third lambda ab which tells your function how to compose a and b:

fun compose3(a: (Int, Int) -> Int, 
             b: (Int, Int) -> Int, 
             ab: (Int, Int) -> Int) = { 
    c: Int, d: Int -> ab(a(c, d), b(c, d))
}

The result is, again, a lambda which takes two Ints and returns one Int.

这篇关于Kotlin高阶函数组成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆