Skip to content
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

Rewrite functional iterators into for loop statement #1952

Closed
kirugan opened this issue Jul 16, 2024 · 2 comments · Fixed by #1954
Closed

Rewrite functional iterators into for loop statement #1952

kirugan opened this issue Jul 16, 2024 · 2 comments · Fixed by #1954

Comments

@kirugan
Copy link
Contributor

kirugan commented Jul 16, 2024

In go 1.23 they accepted functional iterators. We haven't used for loop statement because it wasn't clear what final interface would look like.
One can find all functional iterators by searching usages of iter.Seq or iter.Seq2 types.
Example:

eventsIt(func(res *spec.EventsResponse) bool {
	switch v := res.EventMessage.(type) {
	case *spec.EventsResponse_Event:
		events = append(events, v.Event)
		return true
	case *spec.EventsResponse_Fin:
		return false
	default:
		s.log.Warnw("Unexpected EventMessage from getEvents", "v", v)
		return false
	}
})

to be:

loop:
for res := range eventsIt {
	switch v := res.EventMessage.(type) {
	case *spec.EventsResponse_Event:
		events = append(events, v.Event)
	case *spec.EventsResponse_Fin:
                break loop // mind the loop label, because just break key word doesn't affect iteration
	default:
		s.log.Warnw("Unexpected EventMessage from getEvents", "v", v)
		break loop
	}
})

or

for res := range eventsIt {
	switch v := res.EventMessage.(type) {
	case *spec.EventsResponse_Event:
		events = append(events, v.Event)
                continue // as far as I remember continue works fine inside select
	case *spec.EventsResponse_Fin:
                // will break in the end
	default:
		s.log.Warnw("Unexpected EventMessage from getEvents", "v", v)
	}
        // comment why
	break
})

P.S. First you need to add GOEXPERIMENT=rangefunc env var. I suggest to put it on top of Makefile

@AnkushinDaniil
Copy link
Contributor

Added env var

@obasekiosa
Copy link
Contributor

@kirugan
anyone working on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants