Re: [go-nuts] Better generator support

2020-10-27 Thread Li
You can use a closure as a generator: package main import "fmt" func getPositiveOdds( numbers []int, ) ( iter func() (int, bool), ) { iter = func() (ret int, ok bool) { for len(numbers) > 0 { if numbers[0] > 0 && numbers[0]&1 == 1 { ret = numbers[

Re: [go-nuts] Better generator support

2020-10-26 Thread 'Axel Wagner' via golang-nuts
On Mon, Oct 26, 2020, 19:05 Oliver Smith wrote: > > Hi Axel, thanks for replying; > > It isn't a pattern I teach anyone, rather it's a pattern which people I'm > encouraging to learn golang ask me about. Frequently. I was also under the > impression that generally passing a send-only channel to a

Re: [go-nuts] Better generator support

2020-10-26 Thread Oliver Smith
Hi Axel, thanks for replying; It isn't a pattern I teach anyone, rather it's a pattern which people I'm encouraging to learn golang ask me about. Frequently. I was also under the impression that generally passing a send-only channel to a function could typically be considered an indicator the

Re: [go-nuts] Better generator support

2020-10-25 Thread 'Axel Wagner' via golang-nuts
On Mon, Oct 26, 2020 at 12:29 AM Oliver Smith < oliver.sm...@superevilmegacorp.com> wrote: > This pattern/idiom is very un-golike in that it's an eyesore, and one of > the hardest things I've found to teach people new to trying to read go code. > FWIW, I definitely disagree that this is somehow "

[go-nuts] Better generator support

2020-10-25 Thread Oliver Smith
Looking at how C++ has chosen to provide it's users with generators served as a bit of a wake-up call for me on how we implement them in Go. We write them backwards: ```go func getPositiveOdds(numbers []int) <-chan int { channel := make(chan int) go func () { defer close(channel)