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

A new times method on numeric types #2818

Closed
wants to merge 1 commit into from
Closed

Conversation

bstrie
Copy link
Contributor

@bstrie bstrie commented Jul 6, 2012

This method is intended to elegantly subsume two common iteration functions.
The first is iter::repeat, which is used identically to the method introduced
in this commit, but currently works only on uints. The second is a common case
of {int, i8, uint, etc.}::range, in the case where the inductive variable is
ignored. Compare the usage of the three:

for iter::repeat(100u) {
    // do whatever
}

for int::range(0, 100) |_i| {
    // do whatever
}

for 100.times {
    // do whatever
}

I feel that the latter reads much more nicely than the first two approaches,
and unlike the first two the new method allows the user to ignore the specific
type of the number (ineed, if we're throwing away the inductive variable, who
cares what type it is?). A minor benefit is that this new method will be
somewhat familiar to users of Ruby, from which we borrow the name "times".

This method is intended to elegantly subsume two common iteration functions.
The first is `iter::range`, which is used identically to the method introduced
in this commit, but currently works only on uints. The second is a common case
of `{int, i8, uint, etc.}::range`, in the case where the inductive variable is
ignored. Compare the usage of the three:
```
for iter::range(100u) {
    // do whatever
}

for int::range(0, 100) |_i| {
    // do whatever
}

for 100.times {
    // do whatever
}
```
I feel that the latter reads much more nicely than the first two approaches,
and unlike the first two the new method allows the user to ignore the specific
type of the number (ineed, if we're throwing away the inductive variable, who
cares what type it is?). A minor benefit is that this new method will be
somewhat familiar to users of Ruby, from which we borrow the name "times".
@bstrie
Copy link
Contributor Author

bstrie commented Jul 6, 2012

I'd be happy to go through and update the codebase if this commit is accepted. There look to be about 30 places where iter::repeat is used, and I think about a third of all uses of {int, uint}::range could be replaced.

@brson
Copy link
Contributor

brson commented Jul 6, 2012

I like this, and think we should completely remove iter::repeat. A to method would also be cool - for 10.to(20) |i| {

@brson
Copy link
Contributor

brson commented Jul 6, 2012

Pushed to incoming.

@brson brson closed this Jul 6, 2012
@eholk
Copy link
Contributor

eholk commented Jul 6, 2012

What about replacing uint::range(0, 100) |i| {...} with (0, 100).each |i| {...} too?

@kud1ing
Copy link

kud1ing commented Jul 6, 2012

Would (0, 100) be a tuple or a range constructor?

@eholk
Copy link
Contributor

eholk commented Jul 6, 2012

(0, 100) is a tuple, but we'd make the each method treat it as a range. I guess it might be better as an enum, range(0, 100).each, but then that's strictly more verbose. On second thought, I'm not sure I like my suggestion anymore.

@kud1ing
Copy link

kud1ing commented Jul 6, 2012

I think such use of tuples would be confusing. Some addtional syntactic sugar à la (0 .. 100) would be nice.

saethlin pushed a commit to saethlin/rust that referenced this pull request Apr 11, 2023
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 this pull request may close these issues.

4 participants