Skip to content

Commit d880c3f

Browse files
Philippe-Choletphimuemue
authored andcommitted
ZipLongest::rfold
1 parent 5a321ca commit d880c3f

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/zip_longest.rs

+31
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,37 @@ where
8787
Less => self.b.next_back().map(EitherOrBoth::Right),
8888
}
8989
}
90+
91+
fn rfold<B, F>(self, mut init: B, mut f: F) -> B
92+
where
93+
F: FnMut(B, Self::Item) -> B,
94+
{
95+
let Self { mut a, mut b } = self;
96+
let a_len = a.len();
97+
let b_len = b.len();
98+
match a_len.cmp(&b_len) {
99+
Equal => {}
100+
Greater => {
101+
init = a
102+
.by_ref()
103+
.rev()
104+
.take(a_len - b_len)
105+
.map(EitherOrBoth::Left)
106+
.fold(init, &mut f)
107+
}
108+
Less => {
109+
init = b
110+
.by_ref()
111+
.rev()
112+
.take(b_len - a_len)
113+
.map(EitherOrBoth::Right)
114+
.fold(init, &mut f)
115+
}
116+
}
117+
a.rfold(init, |acc, item_a| {
118+
f(acc, EitherOrBoth::Both(item_a, b.next_back().unwrap()))
119+
})
120+
}
90121
}
91122

92123
impl<T, U> ExactSizeIterator for ZipLongest<T, U>

0 commit comments

Comments
 (0)