Skip to content

Clarify behavior of map/filter #30632

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

Closed
Stebalien opened this issue Dec 30, 2015 · 2 comments
Closed

Clarify behavior of map/filter #30632

Stebalien opened this issue Dec 30, 2015 · 2 comments

Comments

@Stebalien
Copy link
Contributor

Apparently iter::Map and iter::Filter implement DoubleEndedIterator. This could lead to unexpected behavior:

let counter = iter::repeat(()).map({
    let mut ctr = 0;
    move |_| {
        ctr += 1;
        ctr
    }
});

let counter2 = counter.reverse(); // Doesn't reverse anything.

These adapters are only really reversible when passed closure is side-effect free. Unfortunately, we can't simply say that map calls the passed closure on each element yielded because counter.last() != counter.reverse().next().

@steveklabnik
Copy link
Member

/cc @rust-lang/libs

@Kimundi
Copy link
Member

Kimundi commented Jan 1, 2016

I don't think we need to change anything here apart from maybe improving the docs by pointing out this pitfall.

The behavior of last(), reverse() etc is well specified, so different behavior if combined with side-effecting closures should be too.

@brson brson added the A-docs label Jan 11, 2016
steveklabnik added a commit to steveklabnik/rust that referenced this issue Feb 1, 2016
Manishearth added a commit to Manishearth/rust that referenced this issue Feb 2, 2016
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.
tslnc04 added a commit to tslnc04/rust that referenced this issue May 5, 2024
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.
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

No branches or pull requests

4 participants