咖喱怎么能去? [英] How can go-lang curry?

查看:73
本文介绍了咖喱怎么能去?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在像Haskell这样的函数式编程中,我可以定义函数

In functional programming likes Haskell, I can define function

add a b = a+b

然后 add 3 将返回一个带有一个参数的函数,并返回 3 +某物

Then add 3 will return a function that take one parameter and will return 3 + something

如何在GO中执行此操作?

How can I do this in GO?

当我定义一个带有多个(例如n个)参数的函数时,我只能给它一个参数而得到另一个带有n-1个参数的函数吗?

When I define a function that take more than one (say n) parameters, can I only give it one parameter and get another function that take n-1 parameters?

更新:

很抱歉我原来的问题中的单词不准确.

Sorry for the imprecise words in my original question.

我认为我的问题应该作为两个要求来提出:

I think my question should be asked as two qeustions:

  • GO中是否有部分应用程序?
  • 咖喱功能如何发挥作用?

感谢TheOnly92和Alex解决了我的第二个问题.但是,我也对第一个问题感到好奇.

Thanks TheOnly92 and Alex for solving my second question. However, I am also curious about the first question.

推荐答案

扩展上一个答案,使您可以接受任意数量的参数:

To extend on the previous answer, which allows you to take an arbitrary number of arguments:

package main

import (
    "fmt"
)

func mkAdd(a int) func(...int) int {
    return func(b... int) int {
        for _, i := range b {
            a += i
        }
        return a
    }
}

func main() {
    add2 := mkAdd(2)
    add3 := mkAdd(3)
    fmt.Println(add2(5,3), add3(6))
}

这篇关于咖喱怎么能去?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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