Skip to content

Commit

Permalink
Implement Unique::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 db0c386
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/unique_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,23 @@ where
})
}

fn fold<B, F>(mut self, init: B, mut f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
let UniqueBy { iter, used, .. } = &mut self.iter;
iter.fold(init, |mut acc, v| {
if let Entry::Vacant(e) = used.entry(v) {
let elt = e.key().clone();
e.insert(());
acc = f(acc, elt);
}

acc
})
}

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

0 comments on commit db0c386

Please sign in to comment.