douchongbang6011 2013-10-16 03:45
浏览 24
已采纳

咖喱怎么能去?

In functional programming likes Haskell, I can define function

add a b = a+b

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

How can I do this in GO?

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?

Update:

Sorry for the imprecise words in my original question.

I think my question should be asked as two qeustions:

  • Is there partial application in GO?
  • How GO do function curry?

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

  • 写回答

3条回答 默认 最新

  • dre93205 2013-10-16 05:59
    关注

    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))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等