Merge pull request #8 from rampantmonkey/and_then

and_then_*
This commit is contained in:
bluss
2017-03-24 13:40:49 +00:00
committed by GitHub
+44 -2
View File
@@ -216,7 +216,8 @@ impl<L, R> Either<L, R> {
}
}
/// Apply the function `f` on the value in the `Left` variant if it is present.
/// Apply the function `f` on the value in the `Left` variant if it is present rewrapping the
/// result in `Left`.
///
/// ```
/// use either::*;
@@ -236,7 +237,8 @@ impl<L, R> Either<L, R> {
}
}
/// Apply the function `f` on the value in the `Right` variant if it is present.
/// Apply the function `f` on the value in the `Right` variant if it is present rewrapping the
/// result in `Right`.
///
/// ```
/// use either::*;
@@ -281,6 +283,46 @@ impl<L, R> Either<L, R> {
Right(r) => g(r),
}
}
/// Apply the function `f` on the value in the `Left` variant if it is present.
///
/// ```
/// use either::*;
///
/// let left: Either<_, u32> = Left(123);
/// assert_eq!(left.left_and_then::<_,()>(|x| Right(x * 2)), Right(246));
///
/// let right: Either<u32, _> = Right(123);
/// assert_eq!(right.left_and_then(|x| Right::<(), _>(x * 2)), Right(123));
/// ```
pub fn left_and_then<F, S>(self, f: F) -> Either<S, R>
where F: FnOnce(L) -> Either<S, R>
{
match self {
Left(l) => f(l),
Right(r) => Right(r),
}
}
/// Apply the function `f` on the value in the `Right` variant if it is present.
///
/// ```
/// use either::*;
///
/// let left: Either<_, u32> = Left(123);
/// assert_eq!(left.right_and_then(|x| Right(x * 2)), Left(123));
///
/// let right: Either<u32, _> = Right(123);
/// assert_eq!(right.right_and_then(|x| Right(x * 2)), Right(246));
/// ```
pub fn right_and_then<F, S>(self, f: F) -> Either<L, S>
where F: FnOnce(R) -> Either<L, S>
{
match self {
Left(l) => Left(l),
Right(r) => f(r),
}
}
}
/// Convert from `Result` to `Either` with `Ok => Right` and `Err => Left`.