Uh oh!
There was an error while loading. Please reload this page.
Discuss pitfalls of stateful closures with Map - #31220
Conversation
rust-highfive
commented
Jan 26, 2016
(rust_highfive has picked a reviewer for you, use r? to override) |
There was a problem hiding this comment.
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.
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.
Ah, this is a good point. Yes, something like this would be much better.
bluss
commented
Jan 26, 2016
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 |
steveklabnik
commented
Jan 26, 2016
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. |
There was a problem hiding this comment.
Minor nit: using the variable name a when 'a' is being iterated over is slightly confusing
nikomatsakis
commented
Jan 29, 2016
@steveklabnik planning to update? |
steveklabnik
commented
Jan 29, 2016
Yes, later today. On Jan 29, 2016, 15:20 -0500, Niko Matsakisnotifications@github.com, wrote:
|
steveklabnik
commented
Jan 29, 2016
@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. |
nikomatsakis
commented
Feb 1, 2016
I see. Makes sense. But I agree that here printing is natural -- also, this feels like an example that is unlikely to bitrot. |
nikomatsakis
commented
Feb 1, 2016
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., |
nikomatsakis
commented
Feb 1, 2016
r=me either to the current commit or a version like what I suggested, once travis failure is resolved. |
steveklabnik
commented
Feb 1, 2016
I was thinking of the same thing. Tidy passes, and I changed to that. @bors: r=nikomatsakis rollup |
bors
commented
Feb 1, 2016
📌 Commit 6f85be0 has been approved by |
steveklabnik
commented
Feb 1, 2016
Okay. This time I checked that both the tests and |
nikomatsakis
commented
Feb 2, 2016
This is a work of art. @bors r+ rollup |
bors
commented
Feb 2, 2016
📌 Commit 7deb057 has been approved by |
steveklabnik
commented
Feb 2, 2016
❤️ |
Fixesrust-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.