Skip to content

Commit dedb06c

Browse files
committed
Add try_reduce_with
1 parent 2104bc6 commit dedb06c

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/iter/mod.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,35 @@ pub trait ParallelIterator: Sized + Send {
708708
try_reduce::try_reduce(self, identity, op)
709709
}
710710

711+
/// TODO
712+
fn try_reduce_with<T, OP>(self, op: OP) -> Option<Self::Item>
713+
where OP: Fn(T, T) -> Self::Item + Sync + Send,
714+
Self::Item: Try<Ok = T>,
715+
<Self::Item as Try>::Ok: Send,
716+
<Self::Item as Try>::Error: Send
717+
{
718+
let result = self.try_fold(
719+
|| None,
720+
|opt_a, try_b| match (opt_a, try_b.into_result()) {
721+
(Some(a), Ok(b)) => op(a, b).into_result().map(Some),
722+
(_, res_b) => res_b.map(Some),
723+
},
724+
).try_reduce(
725+
|| None,
726+
|opt_a, opt_b| match (opt_a, opt_b) {
727+
(Some(a), Some(b)) => op(a, b).into_result().map(Some),
728+
(Some(v), None) | (None, Some(v)) => Ok(Some(v)),
729+
(None, None) => Ok(None),
730+
},
731+
);
732+
733+
match result {
734+
Ok(None) => None,
735+
Ok(Some(v)) => Some(Self::Item::from_ok(v)),
736+
Err(e) => Some(Self::Item::from_error(e)),
737+
}
738+
}
739+
711740
/// Parallel fold is similar to sequential fold except that the
712741
/// sequence of items may be subdivided before it is
713742
/// folded. Consider a list of numbers like `22 3 77 89 46`. If

0 commit comments

Comments
 (0)