itertools icon indicating copy to clipboard operation
itertools copied to clipboard

`Itertools::dedup_by[_key]` vs `Vec`

Open Philippe-Cholet opened this issue 1 year ago • 1 comments

First, Itertools::dedup_by[_with_count] uses FnMut(&Self::Item, &Self::Item) -> bool while Vec::dedup_by uses FnMut(&mut T, &mut T) -> bool. It differs in mutability: & vs &mut. Not sure why yet.

Second, Vec::dedup_by_key exists but Itertools::dedup_by_key[_with_count] do not:

trait Itertools {
    fn dedup_by_key<F, K>(self, key: F) -> DedupByKey<Self, F>
    where
        Self: Sized,
        F: FnMut(&/*mut ??*/ Self::Item) -> K,
        K: PartialEq;

    fn dedup_by_key_with_count<F, K>(self, key: F) -> DedupByKeyWithCount<Self, F>
    where
        Self: Sized,
        F: FnMut(&/*mut ??*/ Self::Item) -> K,
        K: PartialEq;
}

Philippe-Cholet avatar May 04 '24 08:05 Philippe-Cholet

The Vec methods were added in rust-lang/rust#36743 but there doesn't seem to be an explanation for the choice of &mut. It does make the method slightly more general without affecting the implementation. I do not see any harm in letting the references be mutable in the iterator version. On the other hand Itertools already has the more general (both conceptually and in terms of implementation) coalesce_by() so it seems unlikely that someone would really need this feature.

ronnodas avatar Mar 10 '25 12:03 ronnodas