You don’t need co routines if you have real concurrency. The generator use case is simply an optimization that wouldn’t be necessary if the concurrency model was more efficient - and there was a more expressive way to use it. I demonstrated on some Java boards that generators are easily implemented without exposing the co routines that make lightweight threading possible. The Go implementation get some compiler support which makes it more efficient. 

I think the Java loom project has proved that the Go concurrency model is ideal. Reading up on “structured concurrency” is a great exercise. 

On Feb 24, 2025, at 8:53 PM, Jason E. Aten <j.e.a...@gmail.com> 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 with a "processor" function that accepts an iter.Seq and returns a result:
    func processor(seq iter.Seq[value]) result { ... }

First using channels:

package main
func 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.go
consumer sees  0
consumer sees  1
consumer sees  2
producer 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.

--
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/F1D194CE-F13C-42FF-A9A8-DBB8EFD122BD%40ix.netcom.com.

Reply via email to