douzhulan1815 2018-09-04 12:10
浏览 40

使用闭包在Go中编写下一个置换,我的代码有什么问题

I have written a function "iterPermutation" which uses closure. I want to return array and boolean from the closure which I could not do. So tried only array but it still gives an error

cannot use func literal (type func() []int) as type []int in return argument

I want to use iterPermutation like

a := []int{0,1,2,3,4}
nextPermutation, exists := iterPermutation(a)
for exists {
    nextPermutation()
}

func iterPermutation(a []int) []int {
    return func() []int {
        i := len(a) - 2

        for i >= 0 && a[i+1] <= a[i] {
            i--
        }
        if i < 0 {
            return a
        }

        j := len(a) - 1
        for j >= 0 && a[j] <= a[i] {
            j--
        }

        a[i], a[j] = a[j], a[i]

        for k, l := i, len(a)-1; k < l; k, l = k+1, l-1 {
            a[k], a[l] = a[l], a[k]
        }
        return a
    }
}
  • 写回答

2条回答 默认 最新

  • dpikoto468637 2018-09-04 12:18
    关注

    Golang spec for Return statements described:

    The return value or values may be explicitly listed in the "return" statement. Each expression must be single-valued and assignable to the corresponding element of the function's result type.

    The function called for permutation should contains two values in return one for the array and another for the boolean. Since you are assigning two variables from the function return:

    a := []int{0,1,2,3,4}
    nextPermutation, exists := iterPermutation(a) // it should return two values one for nextPermutation which is an array and other is exists which might be a boolean value.
    for exists {
        nextPermutation()
    }
    

    For below error:

    "cannot use func literal (type func() []int) as type []int in return argument"

    you are returning func() literal enclosed inside the closure function of permutation along with boolean value, so change the return type as:

    package main
    
    func main(){
        a := []int{0,1,2,3,4}
        nextPermutation, _ := iterPermutation(a)
            nextPermutation()
    }
    
    func iterPermutation(a []int) ((func() []int), bool) { // return both values
        return func() []int {
            i := len(a) - 2
    
            for i >= 0 && a[i+1] <= a[i] {
                i--
            }
            if i < 0 {
                return a
            }
    
            j := len(a) - 1
            for j >= 0 && a[j] <= a[i] {
                j--
            }
    
            a[i], a[j] = a[j], a[i]
    
            for k, l := i, len(a)-1; k < l; k, l = k+1, l-1 {
                a[k], a[l] = a[l], a[k]
            }
            return a
        }, true // add boolean value to return from the function.
    }
    

    Working answer on Playground

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?