Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/zip_longest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,37 @@ where
Less => self.b.next_back().map(EitherOrBoth::Right),
}
}

fn rfold<B, F>(self, mut init: B, mut f: F) -> B
where
F: FnMut(B, Self::Item) -> B,
{
let Self { mut a, mut b } = self;
let a_len = a.len();
let b_len = b.len();
match a_len.cmp(&b_len) {
Equal => {}
Greater => {
init = a
.by_ref()
.rev()
.take(a_len - b_len)
.map(EitherOrBoth::Left)
.fold(init, &mut f)
}
Less => {
init = b
.by_ref()
.rev()
.take(b_len - a_len)
.map(EitherOrBoth::Right)
.fold(init, &mut f)
}
}
a.rfold(init, |acc, item_a| {
f(acc, EitherOrBoth::Both(item_a, b.next_back().unwrap()))
})
}
}

impl<T, U> ExactSizeIterator for ZipLongest<T, U>
Expand Down