今天用golang來作個 Lazy Evaluation.
可以參考一下說明:
http://en.wikipedia.org/wiki/Lazy_evaluation
// hello56
package main
import (
"fmt"
)
var resume chan int
func integers() chan int {
yield := make(chan int)
count := 0
go func() {
for {
yield <- count
count++
}
}()
return yield
}
func generateInteger() int {
return <-resume
}
func main() {
resume = integers()
fmt.Println(generateInteger())
fmt.Println(generateInteger())
fmt.Println(generateInteger())
fmt.Println(generateInteger())
}
執行結果:
./hello56
0
1
2
3