mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 03:55:28 +08:00
59 lines
963 B
Go
59 lines
963 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
type Any interface{}
|
|
type EvalFunc func(Any) (Any, Any)
|
|
|
|
func main() {
|
|
evenFunc := func(state Any) (Any, Any) {
|
|
os := state.(int)
|
|
ns := os + 2
|
|
return os, ns
|
|
}
|
|
even := BuildLazyIntEvaluator(evenFunc, 0)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
fmt.Printf("%vth even: %v\n", i, even())
|
|
}
|
|
}
|
|
|
|
func BuildLazyEvaluator(evalFunc EvalFunc, initState Any) func() Any {
|
|
retValChan := make(chan Any)
|
|
loopFunc := func() {
|
|
var actState Any = initState
|
|
var retVal Any
|
|
for {
|
|
retVal, actState = evalFunc(actState)
|
|
retValChan <- retVal
|
|
}
|
|
}
|
|
retFunc := func() Any {
|
|
return <-retValChan
|
|
}
|
|
go loopFunc()
|
|
return retFunc
|
|
}
|
|
|
|
func BuildLazyIntEvaluator(evalFunc EvalFunc, initState Any) func() int {
|
|
ef := BuildLazyEvaluator(evalFunc, initState)
|
|
return func() int {
|
|
return ef().(int)
|
|
}
|
|
}
|
|
|
|
/* Output:
|
|
0th even: 0
|
|
1th even: 2
|
|
2th even: 4
|
|
3th even: 6
|
|
4th even: 8
|
|
5th even: 10
|
|
6th even: 12
|
|
7th even: 14
|
|
8th even: 16
|
|
9th even: 18
|
|
*/
|