下面是python中的代码:
def print_all(x):
print(x)
return print_all
Run Code Online (Sandbox Code Playgroud)
允许打电话print_all(1)(2)
或 print_all(1)(2)(3)(4)
编辑
python中的另一个例子:
def print_sums(x):
print(x)
def next_sum(y):
return print_sums(x+y)
return next_sum
print_sums(1)(3)(5)
Run Code Online (Sandbox Code Playgroud)
映射到语法:
package main
import "fmt"
type printsumfunctype func(x int) printsumfunctype
func printSums(x int) printsumfunctype {
fmt.Println(x)
var nextSum func(y int) printsumfunctype
nextSum = func(y int) printsumfunctype {
return printSums(x + y)
}
return nextSum
}
func main() {
printSums(1)(2)(3)
}
Run Code Online (Sandbox Code Playgroud)
在 GoLang 中,函数是第一类
1) 在 GoLang 中自引用函数的语法是什么?
2)定义类型的首选命名约定是printsumfunctype
什么?
您定义递归函数类型:
type Printer func(interface{}) Printer
func printAll(x interface{}) Printer {
fmt.Println(x)
return printAll
}
func main() {
printAll(1)("Hello")
}
Run Code Online (Sandbox Code Playgroud)
对于新代码,您将编写
type Sink func(int) Sink // not really different, is it?
func printSums(x int) Sink {
fmt.Println(x)
return func(y int) Sink {
return printSums(x + y)
}
}
Run Code Online (Sandbox Code Playgroud)
一个有用的命名约定是仅在其函数后命名类型。在这两种情况下,Printer
(基本上是函数动词+“er”)都有效。或者,更通用的术语Sink
也适用,因为它是一个黑洞,您可以继续向其中倾倒物品。我认为没有任何规定。这些类型并不常见。只需选择一个描述性名称即可。