Skip to content

Commit 21004ed

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 b743e2a commit 21004ed

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
@@ -731,7 +731,7 @@ macro_rules! impl_tuple_combination {
731731

732732
impl<I, A> Iterator for $C<I>
733733
where I: Iterator<Item = A> + Clone,
734-
I::Item: Clone
734+
A: Clone,
735735
{
736736
type Item = (A, $(ignore_ident!($X, A)),*);
737737

@@ -761,6 +761,26 @@ macro_rules! impl_tuple_combination {
761761
let n = self.iter.count();
762762
checked_binomial(n, K).unwrap() + self.c.count()
763763
}
764+
765+
fn fold<B, F>(self, mut init: B, mut f: F) -> B
766+
where
767+
F: FnMut(B, Self::Item) -> B,
768+
{
769+
let Self { c, item, mut iter } = self;
770+
init = c
771+
.map(|($($X),*,)| {
772+
let z = item.clone().unwrap();
773+
(z, $($X),*)
774+
})
775+
.fold(init, &mut f);
776+
while let Some(z) = iter.next() {
777+
let c: $P<I> = iter.clone().into();
778+
init = c
779+
.map(|($($X),*,)| (z.clone(), $($X),*))
780+
.fold(init, &mut f);
781+
}
782+
init
783+
}
764784
}
765785

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

0 commit comments

Comments
 (0)