New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: Go 2: Generator and yield syntax #56320
Comments
Note that you must remember to cancel Coroutines here. type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func kthLargest(root *TreeNode, k int) int {
i := 1
ctx, cancel := context.WithCancel(context.Background())
gen := ReverseInOrderIterator(ctx, root)
//Note that you must remember to cancel Coroutines here.
// If you forget to cancel Coroutines, there will be no error,
//but it will lead to Coroutines leakage!
defer cancel()
for v := range gen {
if i == k {
return v
}
i++
}
panic(errors.New("unreachable"))
}
func ReverseInOrderIterator(ctx context.Context, root *TreeNode) (gen chan int) {
gen = make(chan int)
go func() {
defer close(gen)
if root == nil {
return
}
select {
case <-ctx.Done():
return
default:
{}
}
YieldAll(ctx, gen, ReverseInOrderIterator(ctx, root.Right))
YieldOne(
ctx, gen, root.Val)
YieldAll(ctx, gen, ReverseInOrderIterator(ctx, root.Left))
}()
return
}
func YieldAll(ctx context.Context, target chan int, source chan int) {
for v := range source {
YieldOne(
ctx, target, v)
}
}
func YieldOne(ctx context.Context, target chan int, source int) {
select {
case <-ctx.Done():
return
default:
target <- source
}
} |
I'm sorry, I don't understand what the actual proposal is here. I see Kotlin code, and I see existing Go code, but I don't see any suggested new Go code. You may find it interesting to read the appendix of #54245 as it covers a similar topic. |
runtime.SetFinalizer(r, (*genIter[E]).Stop) Notice the phrase "runtime. SetFinalizer", this is the biggest trouble with using generators under the current syntax! |
Thanks, I understand the problem with the current syntax. What I don't understand is what you are proposing. |
Timed out in state WaitingForInfo. Closing. (I am just a bot, though. Please speak up if this is a mistake or you have the requested information.) |
Author background
generator syntax is built into many popular languages, such as JavaScript, python, kotlin; In Go, to implement such a similar function requires the help of coroutines, which makes the code very cumbersome, and it is not possible to automatically garbage collect the coroutines where the generator is located as other languages do, and if you forget to close the coroutine, it will cause the coroutine to leak.
I think the new generator syntax may be able to learn from JavaScript's generator syntax, which can be used without Coroutines and can be used for automatic garbage collection.
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Generator
https://docs.python.org/3/reference/expressions.html?highlight=yield#yield-expressions
https://kotlinlang.org/docs/sequences.html#from-chunks
Related proposals
Proposal
Show example code before and after the change.
Costs
The text was updated successfully, but these errors were encountered: