請問這題我該如何用Golang去實現

 Please implement a pipe function in a language that you are good at. The function parameter is of indefinite length, the first parameter is a variable of any type, and the following parameter is the function pointer. Please attach unit test.

**

e.g.

def increment (int value) {

return value + 1

}

pipe(5, increment) => 6, pipe(5, increment, increment, increment) => 8

**

godruoyi
最佳答案
package main

import (
    "fmt"
)

func main() {
    fmt.Println(passable(0, increment, increment, increment, increment)) // 4
}


func increment(a int) int {
    a += 1

    return a
}


func passable(a int, funcs ...func(a int) int) int {
    for _, f := range funcs {
        a = f(a)
    }

    return a
}

Online Palyground

3年前 评论
Koopa (楼主) 3年前
讨论数量: 1
godruoyi
package main

import (
    "fmt"
)

func main() {
    fmt.Println(passable(0, increment, increment, increment, increment)) // 4
}


func increment(a int) int {
    a += 1

    return a
}


func passable(a int, funcs ...func(a int) int) int {
    for _, f := range funcs {
        a = f(a)
    }

    return a
}

Online Palyground

3年前 评论
Koopa (楼主) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!