123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- package main
- import (
- "fmt"
- "iter"
- )
- var max = 5
- func s0(yield func() bool) {
- for i := 0; i < max; i++ {
- res := yield()
- fmt.Printf("s0 yield(%d) %t\n", i, res)
- }
- }
- func s1(yield func(y int) bool) {
- var res = true
-
- for i := 0; i < max && res; i++ {
- res = yield(i)
- fmt.Printf("s1 yield(%d) %t\n", i, res)
- }
- }
- func s2(yield func(k int, v int) bool) {
- for x, sq := 0, 0; sq < max; x++ {
- sq = x * x
- res := yield(x, sq)
- fmt.Printf("yield(%d, %d) %t\n", x, sq, res)
- }
- }
- func main() {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- next, stop := iter.Pull(s1)
- nw := func() (int, bool) {
- k, ok := next()
- fmt.Printf("next(%d) %t\n", k, ok)
- return k, ok
- }
- for k, ok := nw(); ok; k, ok = next() {
- fmt.Printf("k: %d, ok: %t\n", k, ok)
- if k > max/2 {
- stop()
- }
- }
- }
|