-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
'range' should support an integer operand. #180
Labels
Comments
I don't think it makes sense to iterate over an integer, as an integer isn't a range. Why not just func Between(a, b int) chan int { c := make(chan int); go func() { for a < b { c<- a; a += 1; } }(); return c; } for i := range Between(0, 10) { } ? One could even do fancy things with having Between() return iterfaces such that you could also do Between(0, 1000).By(2) or From(1000).DownTo(20). Much more flexible than building it into the language, and conceptually nicer too, imho. |
That is a nice little generator pattern there, and I like it a lot conceptually. But, I think it's the wrong fit for this particular corner of the language. I'd be happy enough I suppose if there were just an import that had something like you describe. But, let me make my case for why it makes sense in terms of language design and performance. The RangeClause is added to the language for the purpose of substituting for the ForClause. All the other iterations that range does are things one could do with a longer form ForClause, but RangeClause exists to make 'for' more expressive, with less cruft. What is the most common ForClause in the world? for i := 0; i < N; i++ { } In order to express this you really need to be able to provide: start, end, and stride to produce a range of integers. Also, a for-loop is an extremely tight piece of machine code, so you'd be replacing a few cmps and adds with launching a new goroutine and doing channel I/O... and all that I/O would compound-scale as you chain new steps on (so when you add .By(2) you don't cut your workload in half, you double it)... where adding a stride of 2 to range simply gets you the half of the results you wanted with no additional overhead. |
This issue was closed.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
by jesse.dailey:
The text was updated successfully, but these errors were encountered: