Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Make Intersperse[With] lazy #797

Merged
merged 3 commits into from
Nov 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 38 additions & 21 deletions src/intersperse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,20 @@ where
{
element: ElemF,
iter: Fuse<I>,
peek: Option<I::Item>,
/// `peek` is None while no item have been taken out of `iter` (at definition).
/// Then `peek` will alternatively be `Some(None)` and `Some(Some(item))`,
/// where `None` indicates it's time to generate from `element` (unless `iter` is empty).
peek: Option<Option<I::Item>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the added nesting, could you just write a comment here about how the value of peek should be interpreted? (None, Some(None), Some(Some(...))?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. And I will do the same for Product and CoalesceBy before publishing their similar laziness fix.

}

/// Create a new `IntersperseWith` iterator
pub fn intersperse_with<I, ElemF>(iter: I, elt: ElemF) -> IntersperseWith<I, ElemF>
where
I: Iterator,
{
let mut iter = iter.fuse();
IntersperseWith {
peek: iter.next(),
iter,
peek: None,
iter: iter.fuse(),
element: elt,
}
}
Expand All @@ -78,39 +80,54 @@ where
type Item = I::Item;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.peek.is_some() {
self.peek.take()
} else {
self.peek = self.iter.next();
if self.peek.is_some() {
Some(self.element.generate())
} else {
None
let Self {
element,
iter,
peek,
} = self;
match peek {
Some(item @ Some(_)) => item.take(),
Some(None) => match iter.next() {
new @ Some(_) => {
*peek = Some(new);
Some(element.generate())
}
None => None,
},
None => {
*peek = Some(None);
iter.next()
}
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
// 2 * SH + { 1 or 0 }
let has_peek = self.peek.is_some() as usize;
let sh = self.iter.size_hint();
size_hint::add_scalar(size_hint::add(sh, sh), has_peek)
let mut sh = self.iter.size_hint();
sh = size_hint::add(sh, sh);
match self.peek {
Some(Some(_)) => size_hint::add_scalar(sh, 1),
Some(None) => sh,
None => size_hint::sub_scalar(sh, 1),
}
}

fn fold<B, F>(mut self, init: B, mut f: F) -> B
fn fold<B, F>(self, init: B, mut f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
let Self {
mut element,
mut iter,
peek,
} = self;
let mut accum = init;

if let Some(x) = self.peek.take() {
if let Some(x) = peek.unwrap_or_else(|| iter.next()) {
accum = f(accum, x);
}

let element = &mut self.element;

self.iter.fold(accum, |accum, x| {
iter.fold(accum, |accum, x| {
let accum = f(accum, element.generate());
f(accum, x)
})
Expand Down