Skip to content

Commit

Permalink
Implement UniqueBy::fold
Browse files Browse the repository at this point in the history
  • Loading branch information
kinto-b committed Apr 18, 2024
1 parent ed695af commit 5a6fb6f
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/unique_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,26 @@ where
iter.find(|v| used.insert(f(v), ()).is_none())
}

fn fold<B, G>(self, init: B, mut f: G) -> B
where
Self: Sized,
G: FnMut(B, Self::Item) -> B,
{
let Self {
iter,
mut used,
f: mut key,
} = self;

iter.fold(init, |mut acc, v| {
if used.insert(key(&v), ()).is_none() {
acc = f(acc, v);
};

acc
})
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let (low, hi) = self.iter.size_hint();
Expand Down

0 comments on commit 5a6fb6f

Please sign in to comment.