-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Guide: iterators #16408
Guide: iterators #16408
Conversation
result for our sum. `1 + 2 + 3 = 6`, and that's the result we got. | ||
|
||
Whew. `fold` can be a bit strange the first few times you see it, but once it | ||
clicks, you an use it all over the place. Any time you have a list of things, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/an/can/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you!
Nicely written. I expected to learn two more things though:
(Correct me if this is wrong.) |
@killercup great point on the lazy, but we haven't actually introduced traits yet, so we can't talk about that. Implementing iterators is out of the scope of this guide, but should certainly go in the broader iterator documentation. |
Okay. New draft up. This is significantly different, but addresses all of the problems with the last one. What do we think? |
We make a mutable binding to the return value of `range`, which is our iterator. | ||
We then `loop`, with an inner `match`. This `match` is used on the result of | ||
`range.next()`, which gives us a reference to the next value of the iterator. | ||
`next` returns an `Option<int>`, in this case, which will be `Some(int)` when |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the int
in Some(int)
meant to be the type or meant to be a name binding?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the type.
I like it! |
``` | ||
|
||
`fold()` is a consumer that looks like this: | ||
`fold(base, |accumulator, element| result`. It takes two arguments: the first |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing )
. Also, looking at it written down, maybe it could be just fold(base, |accumulator, element| ...)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed.
``` | ||
|
||
This iterator counts up from one, adding five each time. It will give | ||
you a new integer every time, forever. But since iterators are lazy, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be precise, they're not always new as it repeats after 264 (or 232) elements.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a sentence here. Once we address #15526 , I'll make it more specific.
An introduction to iterators. I kinda like this, but I kinda don't. Hmmm.
An introduction to iterators. I kinda like this, but I kinda don't. Hmmm.