Skip to content

Commit

Permalink
Add and_then methods for EitherOrBoth
Browse files Browse the repository at this point in the history
  • Loading branch information
Avi-D-coder committed Dec 23, 2018
1 parent 0bee403 commit e51fb7c
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/either_or_both.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,28 @@ impl<A, B> EitherOrBoth<A, B> {
Both(a, b) => Both(f(a), g(b)),
}
}

/// Apply the function `f` on the value `b` in `Right(b)` or `Both(a, _)` variants if it is
/// present.
pub fn left_and_then<F, L>(self, f: F) -> EitherOrBoth<L, B>
where
F: FnOnce(A) -> EitherOrBoth<L, B>,
{
match self {
Left(a) | Both(a, _) => f(a),
Right(b) => Right(b),
}
}

/// Apply the function `f` on the value `a`
/// in `Left(a)` or `Both(a, _)` variants if it is present.
pub fn right_and_then<F, R>(self, f: F) -> EitherOrBoth<A, R>
where
F: FnOnce(B) -> EitherOrBoth<A, R>,
{
match self {
Left(a) => Left(a),
Right(b) | Both(_, b) => f(b),
}
}
}

0 comments on commit e51fb7c

Please sign in to comment.