Skip to content

Tracking Issue for Iterator::{dedup, dedup_by, dedup_by_key} #83747

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
3 tasks
slerpyyy opened this issue Apr 1, 2021 · 2 comments
Open
3 tasks

Tracking Issue for Iterator::{dedup, dedup_by, dedup_by_key} #83747

slerpyyy opened this issue Apr 1, 2021 · 2 comments
Labels
A-iterators Area: Iterators C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@slerpyyy
Copy link

slerpyyy commented Apr 1, 2021

Feature gate: #![feature(iter_dedup)]

This is a tracking issue for the functions std::iter::Iterator::{dedup, dedup_by, dedup_by_key} as well as the structs std::iter::{Dedup, ByKey, ByPartialEq}.

This feature provides similar functionality as the functions for slices and Vecs with the same name:

let vec = vec![1, 2, 2, 3, 2];

let mut iter = vec.into_iter().dedup();

assert_eq!(iter.next(), Some(1));
assert_eq!(iter.next(), Some(2));
assert_eq!(iter.next(), Some(3));
assert_eq!(iter.next(), Some(2));
assert_eq!(iter.next(), None);

Public API

pub trait Iterator {
    /// Removes all but the first of consecutive elements in the iterator
    /// according to the `PartialEq` trait implementation.
    fn dedup(self) -> Dedup<Self, ByPartialEq>
    where
        Self: Sized,
        Self::Item: PartialEq,
    { ... }
    
    /// Removes all but the first of consecutive elements in the iterator
    /// satisfying a given equality relation.
    fn dedup_by<F>(self, same_bucket: F) -> Dedup<Self, F>
    where
        Self: Sized,
        F: FnMut(&Self::Item, &Self::Item) -> bool,
    { ... }
    
    /// Removes all but the first of consecutive elements in the iterator
    /// that resolve to the same key.
    fn dedup_by_key<F, K>(self, key: F) -> Dedup<Self, ByKey<F>>
    where
        Self: Sized,
        F: FnMut(&Self::Item) -> K,
        K: PartialEq,
    { ... }
}

Steps / History

Unresolved Questions

  • Is there a way to reduce dedup and dedup_by_key to a call to dedup_by without creating an impossible function signature?
  • Should we also implement SourceIter and InPlaceIterable for these structs?
  • How should/can we react to an infinite iterator where all items are the same?
  • Is there a way to turn this into a no-op for iterators which cannot contain duplicates (e.g. hashset.iter().dedup())?
@slerpyyy slerpyyy added C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Apr 1, 2021
@jonas-schievink jonas-schievink added the A-iterators Area: Iterators label Apr 1, 2021
@leonardo-m
Copy link

This is probably going to be rather useful.

@shhra
Copy link

shhra commented Aug 20, 2021

This should be handy when designing language processing algorithms.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-iterators Area: Iterators C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants