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

'range' should support an integer operand. #180

Closed
gopherbot opened this issue Nov 14, 2009 · 5 comments
Closed

'range' should support an integer operand. #180

gopherbot opened this issue Nov 14, 2009 · 5 comments
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language

Comments

@gopherbot
Copy link
Contributor

by jesse.dailey:

The 'range' clause should support an integer as it's right operand.

Such as:

for i := range 10 {
  // do stuff ten times, i = 0,1,...9
}

Or similar.  Analogous to the way the range() iterator works in python.
@rsc
Copy link
Contributor

rsc commented Nov 14, 2009

Comment 1:

Labels changed: added language-change.

Status changed to Thinking.

@gopherbot
Copy link
Contributor Author

Comment 2 by consalus:

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.

@gopherbot
Copy link
Contributor Author

Comment 3 by jesse.dailey:

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.

@gopherbot
Copy link
Contributor Author

Comment 4 by hall.jeff:

I think the original request was basically to default start = 0 and stride = 1... no
real "magic" there

@robpike
Copy link
Contributor

robpike commented Nov 21, 2009

Comment 5:

Status changed to WontFix.

@bradfitz bradfitz added the LanguageChange Suggested changes to the Go language label Dec 17, 2014
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language
Projects
None yet
Development

No branches or pull requests

4 participants