这是一个闭包的例子吗?

huangapple go评论85阅读模式
英文:

Is this a closure example?

问题

我正在阅读Mark Summerfield的书《Go编程》第5.6.3节中的闭包示例。他将闭包定义为“一个函数,它“捕获”在创建它的同一作用域中存在的任何常量和变量,如果它引用它们的话”。

他说闭包的一个用途是匿名函数(或Go中的函数字面量)。

他给出了以下示例:

addPng := func(name string) string { return name + ".png" }
addJpg := func(name string) string { return name + ".jpg" }
fmt.Println(addPng("filename"), addJpg("filename"))

我理解匿名函数addPng是字符串拼接运算符+的一个包装器。

如果我理解正确,他给一个匿名函数赋予一个名称,然后使用该名称调用函数。我不明白这个示例的意义。如果我在main()函数内定义相同的函数addPng并调用它,我会得到相同的结果:

package main

import "fmt"
    
func addPng(name string) string {
	return name + ".png"
}

func main() {
	fmt.Println(addPng("filename"))
}

我知道我不能在另一个函数内定义和使用一个函数。但是为什么Summerfield的示例中的匿名函数被称为“闭包”?为什么要使用一个包装函数?我漏掉了什么?

英文:

I am reading Closure example in Mark Summerfield's book Programming in Go Section 5.6.3. He defines Closure as a "function which "captures" any constants and variables that are present in the same scope where it is created, if it refers to them."

He says that one use of closure is anonymous functions (or function literals in Go)

He gives this examples:

addPng := func(name string) string { return name + ".png" }
addJpg := func(name string) string { return name + ".jpg" }
fmt.Println(addPng("filename"), addJpg("filename"))

I understand that anonymous function named addPng is a wrapper for the string concatenation operator +.

If I understand correctly, he is assigning an anonymous function a name then calling the function with that name. I don't see the point of this example. If I define the same function addPng and call it inside main() I get the same result:

package main

import ("fmt")
    
func addPng (name string) string {
	return name + ".png"
	}

func main() {
	fmt.Println(addPng("filename"))
}

I understand that I cannot define and use a function inside another function. But why is the anonymous function in Summerfield's example called "Closure"? And why use a wrapper function? What am I missing?

答案1

得分: 3

这是使用闭包进行状态表示的一个示例。

package main

import "fmt"

func NextFibonacci() func() int {
    a, b := 0, 1
    return func() (f int) {
        f, a, b = a, b, a+b
        return
    }
}

func main() {
    nf := NextFibonacci()
    f := make([]int, 7)
    for i := range f {
        f[i] = nf()
    }
    fmt.Println(len(f), f)
}

输出:

7 [0 1 1 2 3 5 8]
英文:

Here's an example of using a closure for state representation.

package main

import "fmt"

func NextFibonacci() func() int {
	a, b := 0, 1
	return func() (f int) {
		f, a, b = a, b, a+b
		return
	}
}

func main() {
	nf := NextFibonacci()
	f := make([]int, 7)
	for i := range f {
		f[i] = nf()
	}
	fmt.Println(len(f), f)
}

Output:

7 [0 1 1 2 3 5 8]

答案2

得分: 2

这不是我第一次看到有人提到这本特定的书,其中引用的材料要么是完全错误的,要么是完全错过了重点。

让我在这里停止谈论这本书,并建议不要使用它。

要了解闭包的一个好的和正确的定义,请参阅Wikipedia。注意形容词“词法”。

英文:

It's not for the first time I see someone referring to this particular book where the cited material is either plain wrong or basically missing the point completely.

Let me stop talking about the book here by suggesting to not use it at all.

For a good and correct definition of a closure, see Wikipedia. Pay attention to the adjective 'lexical'.

huangapple
  • 本文由 发表于 2013年7月7日 22:11:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/17512876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定