I don't understand the tradeoffs involved, so I can't speak to the 
hesitancy to release coroutines.  They would probably be great
fun to play with. I imagine, however, they would provide ample foot-guns 
too.
Perhaps that is a part of the reason. I find reading coroutine code in Lua,
for example, a fairly mind-boggling torture exercise.

On Sunday, February 23, 2025 at 12:52:58 AM UTC Nuno Cruces wrote:

I wanted to implement this "push like" interface 
<https://pkg.go.dev/github.com/ncruces/go-sqlite3#AggregateFunction> with a 
"processor" function that accepts an iter.Seq and returns a result:
    func processor(seq iter.Seq[value]) result { ... }


First using channels:






















*package mainfunc consumeAndSum(ch chan int) (sum int) {    for i := range 
ch {        println("consumer sees ", i)        sum += i    }    
println("consumer done.")    return sum}func startProducer() chan int {    
ch := make(chan int)    go func() {        for i := range 3 {            ch 
<- i        }        close(ch)        println("producer exits.")    }()    
return ch}func main() {*
*    ch := startProducer()*
*    sum := consumeAndSum(ch)*
*    println("all done. sum = ", sum)*
*}*

*/*   output:*

*consumer sees  0*

*consumer sees  1*

*producer exits.*

*consumer sees  2*

*consumer done.*

*all done. sum =  3 *

**/*

*Second using iter.Seq:*

*package main*

*import "iter"*
































*func consumeAndSum(it iter.Seq[int]) (sum int) { for i := range it { 
println("consumer sees ", i) sum += i } println("consumer done.") return 
sum}func startProducer() iter.Seq[int] { return func(yield func(int) bool) 
{ for k := range 3 { if !yield(k) { return } } println("producer exits.") 
}}func main() { it := startProducer() sum := consumeAndSum(it) println("all 
done. sum = ", sum)}/* output:go run iter.goconsumer sees  0consumer sees 
 1consumer sees  2producer exits.consumer done.all done. sum =  3*/*

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/6f090b16-ac52-4ea0-b5ea-1f7b3b7e6e7cn%40googlegroups.com.

Reply via email to