Skip to content

Commit 0e15566

Browse files
Tuple*Combinations::fold (macro)
`I::Item` and `A` are the same so the "where condition" is not really changed, but it is apparently needed here. We fold `c` then for each item of `iter`, we fold the cloned `iter`. The `while let` loop should probably be converted to `fold` somehow later (if possible) but the core logic is done and it is already really faster.
1 parent ded12e8 commit 0e15566

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/adaptors/mod.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ macro_rules! impl_tuple_combination {
715715

716716
impl<I, A> Iterator for $C<I>
717717
where I: Iterator<Item = A> + Clone,
718-
I::Item: Clone
718+
A: Clone,
719719
{
720720
type Item = (A, $(ignore_ident!($X, A)),*);
721721

@@ -745,6 +745,26 @@ macro_rules! impl_tuple_combination {
745745
let n = self.iter.count();
746746
checked_binomial(n, K).unwrap() + self.c.count()
747747
}
748+
749+
fn fold<B, F>(self, mut init: B, mut f: F) -> B
750+
where
751+
F: FnMut(B, Self::Item) -> B,
752+
{
753+
let Self { c, item, mut iter } = self;
754+
init = c
755+
.map(|($($X),*,)| {
756+
let z = item.clone().unwrap();
757+
(z, $($X),*)
758+
})
759+
.fold(init, &mut f);
760+
while let Some(z) = iter.next() {
761+
let c: $P<I> = iter.clone().into();
762+
init = c
763+
.map(|($($X),*,)| (z.clone(), $($X),*))
764+
.fold(init, &mut f);
765+
}
766+
init
767+
}
748768
}
749769

750770
impl<I, A> HasCombination<I> for (A, $(ignore_ident!($X, A)),*)

0 commit comments

Comments
 (0)