Skip to content

Commit 3a0f7db

Browse files
committed
Add EitherOrBoth reduce method
1 parent f5bfc64 commit 3a0f7db

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/either_or_both.rs

+14
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,17 @@ impl<A, B> EitherOrBoth<A, B> {
157157
}
158158
}
159159
}
160+
161+
impl<T> EitherOrBoth<T, T> {
162+
/// Return either value of left, right, or the product of `f` applied where `Both` are present.
163+
pub fn reduce<F>(self, f: F) -> T
164+
where
165+
F: FnOnce(T, T) -> T,
166+
{
167+
match self {
168+
Left(a) => a,
169+
Right(b) => b,
170+
Both(a, b) => f(a, b),
171+
}
172+
}
173+
}

0 commit comments

Comments
 (0)