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
- // Stop on our terms, OR if a break was used in the for loop.
- 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() {
- //for range s0 {
- // fmt.Print("In range s0: ")
- //}
- //fmt.Println()
- //
- //for k := range s1 {
- // fmt.Print("In range s1 ")
- // if k > max/2 {
- // break
- // }
- //}
- //fmt.Println()
- //
- //for k := range s2 {
- // fmt.Print("In range s2 ")
- // if k > max/2 {
- // break
- // }
- //}
- //fmt.Println()
- //
- //for k, v := range s2 {
- // fmt.Printf("In range s2 %d/%d ", k, v)
- // if k > max/2 {
- // break
- // }
- //}
- 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()
- }
- }
- }
|