I'm following this presentation. At the very end there's an exercise about solving Fibonacci and says that instead of addition, make the operation setteable by a function.
Is the following is a good solution? How close might it be to what Rob Pike would have written?
package main
import "fmt"
func add(a, b int) int {
return a + b
}
func subtract(a, b int) int {
return a - b
}
func power(a, b int) int {
return a ^ b
}
// fib returns a function that returns
// successive Fibonacci numbers.
func fib(op func(int, int) int) func() int {
n0, n1 := 0, 1
return func() int {
n0, n1 = n1, op(n0, n1)
return n0
}
}
func main() {
f := fib(add)
// Function calls are evaluated left-to-right.
fmt.Println(f(), f(), f(), f(), f(), f(), f())
f = fib(subtract)
// Function calls are evaluated left-to-right.
fmt.Println(f(), f(), f(), f(), f(), f(), f())
f = fib(power)
// Function calls are evaluated left-to-right.
fmt.Println(f(), f(), f(), f(), f(), f(), f())
}