-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Discuss pitfalls of stateful closures with Map #31220
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
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
/// } | ||
/// }); | ||
/// | ||
/// let counter2 = counter.reverse(); |
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.
.rev()
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.
ugh, i fixed it in my test but not when I put it over here. was too worried about the words
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 think I would find the example clearer if you included the items being iterated over, and they were not the numbers 1, 2, and 3. For example:
vec!['a', 'b', 'c'].into_iter().map(...).rev();
will produce ('c', 1), ('b', 2), ('a', 3)
, right?
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.
Ah, this is a good point. Yes, something like this would be much better.
Maybe it can instead underline that the map's closure is called on elements in the order they are "extracted" from the iterator (i.e. if you mix next/next_back or call just next_back etc). Saying that the iteration is not backwards is too confusable with how |
Okay, so i pushed a new commit which actually fails. The weird thing about doing what @nikomatsakis says is that it does illustrate what's happening better, which, uh, makes it feel not weird. And so now this documentation feels strange, because it feels like it's saying "hey this is so counterintuitive"... but it's not. So, I'm kinda almost leaning towards not writing something about this. Yes, writing weird code might be a bit confusing, but you're also writing odd code? I dunno. |
/// // of iterations. | ||
/// let counter = vec!['a', 'b', 'c'].into_iter().map({ | ||
/// let mut count = 0; | ||
/// move |a| { |
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.
Minor nit: using the variable name a
when 'a'
is being iterated over is slightly confusing
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
@steveklabnik planning to update? |
Yes, later today. On Jan 29, 2016, 15:20 -0500, Niko [email protected], wrote:
|
@nikomatsakis I have updated it to your example, with one or two minor wording/format tweaks. I was trying to think of a way to get away from 'printing' examples, as I don't like them as much as assertions, since printing won't be automatically tested, but assertions end up just looking awkward in examples like this, so I don't think I can do much better. |
I see. Makes sense. But I agree that here printing is natural -- also, this feels like an example that is unlikely to bitrot. |
Note travis failure:
Seems legit? The only other nit is that maybe it'd be better to separate the two examples a bit. e.g.,
|
r=me either to the current commit or a version like what I suggested, once travis failure is resolved. |
I was thinking of the same thing. Tidy passes, and I changed to that. @bors: r=nikomatsakis rollup |
📌 Commit 6f85be0 has been approved by |
Okay. This time I checked that both the tests and |
This is a work of art. @bors r+ rollup |
📌 Commit 7deb057 has been approved by |
❤️ |
Fixes rust-lang#30632 I'm not sure if this explanation is good enough. If it is, I will add it to filter as well.
Issue rust-lang#30632 shows the unexpected behavior that can arise from `iter::Map` and `iter::Filter` both implementing `DoubleEndedIterator`. PR rust-lang#31220 makes note of this behavior by adding a note to the docs for `iter::Map`, but does not do the same for `iter::Filter`. This PR adds similar documentation to `iter::Filter`. It may be worth considering adding this documentation to `iter::FilterMap` as well or placing it in another location with a note that it applies to using all of these types with stateful closures.
Fixes #30632
I'm not sure if this explanation is good enough. If it is, I will add it to filter as well.