Skip to content

Commit

Permalink
TakeWhileInclusive::fold
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe-Cholet authored and phimuemue committed Jan 14, 2024
1 parent 0ed6f79 commit f60fe7e
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/take_while_inclusive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ where
(0, self.iter.size_hint().1)
}
}

fn fold<B, Fold>(mut self, init: B, mut f: Fold) -> B
where
Fold: FnMut(B, Self::Item) -> B,
{
if self.done {
init
} else {
let predicate = &mut self.predicate;
self.iter
.try_fold(init, |mut acc, item| {
let is_ok = predicate(&item);
acc = f(acc, item);
if is_ok {
Ok(acc)
} else {
Err(acc)
}
})
.unwrap_or_else(|err| err)
}
}
}

impl<I, F> FusedIterator for TakeWhileInclusive<I, F>
Expand Down

0 comments on commit f60fe7e

Please sign in to comment.