Skip to content

Commit e8ca207

Browse files
committed
Add and_then methods for EitherOrBoth
1 parent afd731c commit e8ca207

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/either_or_both.rs

+24
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,28 @@ impl<A, B> EitherOrBoth<A, B> {
132132
Both(a, b) => Both(f(a), g(b)),
133133
}
134134
}
135+
136+
/// Apply the function `f` on the value `b` in `Right(b)` or `Both(a, _)` variants if it is
137+
/// present.
138+
pub fn left_and_then<F, L>(self, f: F) -> EitherOrBoth<L, B>
139+
where
140+
F: FnOnce(A) -> EitherOrBoth<L, B>,
141+
{
142+
match self {
143+
Left(a) | Both(a, _) => f(a),
144+
Right(b) => Right(b),
145+
}
146+
}
147+
148+
/// Apply the function `f` on the value `a`
149+
/// in `Left(a)` or `Both(a, _)` variants if it is present.
150+
pub fn right_and_then<F, R>(self, f: F) -> EitherOrBoth<A, R>
151+
where
152+
F: FnOnce(B) -> EitherOrBoth<A, R>,
153+
{
154+
match self {
155+
Left(a) => Left(a),
156+
Right(b) | Both(_, b) => f(b),
157+
}
158+
}
135159
}

0 commit comments

Comments
 (0)