I'm… sorry. I don't understand the purpose of this code listing. In my original post I gave a solution to my own problem implemented in two ways: with goroutines and channels, and with newcoro and coroswitch. If you're trying to show me that I don't need either of those, that iter.Seq is enough, I would kindly ask that you reconsider, and try to implement my interface that way. I don't think it's possible, but I'll be really happy if you can prove me wrong.
If you want I can explain a little further the purpose of this, just so this don't feel like this is an X/Y problem, but the motivation for this is very similar to iter.Pull (which similarly can't be written in Go without goroutines): iter.Seq is not the last word in iteration interfaces; other interfaces may desirably need to be adapted to it. Regards, Nuno On Tuesday 25 February 2025 at 02:52:24 UTC Jason E. Aten wrote: > 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/9867e325-f7e1-411c-b7a1-5b06532c3145n%40googlegroups.com.