Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a number of methods to EitherOrBoth #717

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
261 changes: 261 additions & 0 deletions src/either_or_both.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,240 @@ impl<A, B> EitherOrBoth<A, B> {
}
}

/// Apply the function `f` on the value `a` in `Left(a)` or `Both(a, _)` variants. If the
/// function returns `true`, the value is kept, otherwise, the value is filtered out.
///
/// # Examples
///
/// ```
/// # use itertools::EitherOrBoth;
/// assert_eq!(
/// EitherOrBoth::Both("tree", 1).filter_left(|l| *l == "tree"),
/// Some(EitherOrBoth::Both("tree", 1))
/// );
/// assert_eq!(
/// EitherOrBoth::Both("tree", 1).filter_left(|l| *l != "tree"),
/// Some(EitherOrBoth::Right(1))
/// );
/// assert_eq!(
/// EitherOrBoth::<_, ()>::Left("tree").filter_left(|l| *l == "tree"),
/// Some(EitherOrBoth::Left("tree"))
/// );
/// assert_eq!(
/// EitherOrBoth::<_, ()>::Left("tree").filter_left(|l| *l != "tree"),
/// None
/// );
/// ```
pub fn filter_left<F>(self, f: F) -> Option<EitherOrBoth<A, B>>
where
F: FnOnce(&A) -> bool,
{
match self {
Left(a) => if f(&a) { Some(Left(a)) } else { None },
Right(b) => Some(Right(b)),
Both(a, b) => if f(&a) { Some(Both(a, b)) } else { Some(Right(b)) },
}
}

/// Apply the function `f` on the value `b` in `Right(b)` or `Both(_, b)` variants. If the
/// function returns `true`, the value is kept, otherwise, the value is filtered out.
///
/// # Examples
///
/// ```
/// # use itertools::EitherOrBoth;
/// assert_eq!(
/// EitherOrBoth::Both("tree", 1).filter_right(|r| *r == 1),
/// Some(EitherOrBoth::Both("tree", 1))
/// );
/// assert_eq!(
/// EitherOrBoth::Both("tree", 1).filter_right(|r| *r != 1),
/// Some(EitherOrBoth::Left("tree"))
/// );
/// assert_eq!(
/// EitherOrBoth::<(), _>::Right(1).filter_right(|r| *r == 1),
/// Some(EitherOrBoth::Right(1))
/// );
/// assert_eq!(
/// EitherOrBoth::<(), _>::Right(1).filter_right(|r| *r != 1),
/// None
/// );
/// ```
pub fn filter_right<F>(self, f: F) -> Option<EitherOrBoth<A, B>>
where
F: FnOnce(&B) -> bool,
{
match self {
Left(a) => Some(Left(a)),
Right(b) => if f(&b) { Some(Right(b)) } else { None },
Both(a, b) => if f(&b) { Some(Both(a, b)) } else { Some(Left(a)) },
}
}

/// Apply the functions `f` and `g` on the value `a` and `b` respectively;
/// found in `Left(a)`, `Right(b)`, or `Both(a, b)` variants. If the result
/// of function `f` is `true`, the value `a` is kept, otherwise, the value
/// is filtered out. If the result of function `g` is `true`, the value `b`
/// is kept, otherwise the value is filtered out.
///
/// # Examples
///
/// ```
/// # use itertools::EitherOrBoth;
/// assert_eq!(
/// EitherOrBoth::Both("tree", 1).filter_any(|l| *l == "tree", |r| *r == 1),
/// Some(EitherOrBoth::Both("tree", 1))
/// );
/// assert_eq!(
/// EitherOrBoth::Both("tree", 1).filter_any(|l| *l != "tree", |r| *r == 1),
/// Some(EitherOrBoth::Right(1))
/// );
/// assert_eq!(
/// EitherOrBoth::Both("tree", 1).filter_any(|l| *l == "tree", |r| *r != 1),
/// Some(EitherOrBoth::Left("tree"))
/// );
/// assert_eq!(
/// EitherOrBoth::Both("tree", 1).filter_any(|l| *l != "tree", |r| *r != 1),
/// None
/// );
/// ```
pub fn filter_any<F, G>(self, f: F, g: G) -> Option<EitherOrBoth<A, B>>
where
F: FnOnce(&A) -> bool,
G: FnOnce(&B) -> bool,
{
match self {
Left(a) => if f(&a) { Some(Left(a)) } else { None },
Right(b) => if g(&b) { Some(Right(b)) } else { None },
Both(a, b) => match (f(&a), g(&b)) {
(false, false) => None,
(true, false) => Some(Left(a)),
(false, true) => Some(Right(b)),
(true, true) => Some(Both(a, b)),
}
}
}

/// Apply the function `f` on the value `a` in `Left(a)` or `Both(a, _)` variants. If the
/// function returns `Some(value)`, the value `a` is replaced, otherwise, the value `a` is
/// filtered out.
///
/// # Examples
///
/// ```
/// # use std::str::FromStr;
/// # use itertools::EitherOrBoth;
/// assert_eq!(
/// EitherOrBoth::Both("1", 1).filter_map_left(|l| u16::from_str(l).ok()),
/// Some(EitherOrBoth::Both(1u16, 1))
/// );
/// assert_eq!(
/// EitherOrBoth::Both("tree", 1).filter_map_left(|l| u16::from_str(l).ok()),
/// Some(EitherOrBoth::Right(1))
/// );
/// assert_eq!(
/// EitherOrBoth::<_, ()>::Left("1").filter_map_left(|l| u16::from_str(l).ok()),
/// Some(EitherOrBoth::Left(1u16))
/// );
/// assert_eq!(
/// EitherOrBoth::<_, ()>::Left("tree").filter_map_left(|l| u16::from_str(l).ok()),
/// None
/// );
/// ```
pub fn filter_map_left<F, L>(self, f: F) -> Option<EitherOrBoth<L, B>>
where
F: FnOnce(A) -> Option<L>,
{
match self {
Left(a) => f(a).map(Left),
Right(b) => Some(Right(b)),
Both(a, b) => if let Some(l) = f(a) { Some(Both(l, b)) } else { Some(Right(b)) },
}
}

/// Apply the function `f` on the value `b` in `Right(b)` or `Both(_, b)` variants. If the
/// function returns `Some(value)`, the value `b` is replaced, otherwise, the value `b` is
/// filtered out.
///
/// # Examples
///
/// ```
/// # use std::convert::TryFrom;
/// # use itertools::EitherOrBoth;
/// assert_eq!(
/// EitherOrBoth::Both("1", 1).filter_map_right(|r| u8::try_from(r).ok()),
/// Some(EitherOrBoth::Both("1", 1u8))
/// );
/// assert_eq!(
/// EitherOrBoth::Both("1", 1024).filter_map_right(|r| u8::try_from(r).ok()),
/// Some(EitherOrBoth::Left("1"))
/// );
/// assert_eq!(
/// EitherOrBoth::<(), _>::Right(1).filter_map_right(|r| u8::try_from(r).ok()),
/// Some(EitherOrBoth::Right(1u8))
/// );
/// assert_eq!(
/// EitherOrBoth::<(), _>::Right(1024).filter_map_right(|r| u8::try_from(r).ok()),
/// None
/// );
/// ```
pub fn filter_map_right<F, R>(self, f: F) -> Option<EitherOrBoth<A, R>>
where
F: FnOnce(B) -> Option<R>,
{
match self {
Left(a) => Some(Left(a)),
Right(b) => f(b).map(Right),
Both(a, b) => if let Some(r) = f(b) { Some(Both(a, r)) } else { Some(Left(a)) },
}
}

/// Apply the functions `f` and `g` on the value `a` and `b` respectively;
/// found in `Left(a)`, `Right(b)`, or `Both(a, b)` variants. If the result
/// of function `f` is `Some(value)`, the value `a` is replaced, otherwise,
/// the value is filtered out. If the result of function `g` is `Some(value)`,
/// the value `b` is replaced, otherwise the value is filtered out.
///
/// # Examples
///
/// ```
/// # use std::str::FromStr;
/// # use std::convert::TryFrom;
/// # use itertools::EitherOrBoth;
/// assert_eq!(
/// EitherOrBoth::Both("1", 1).filter_map_any(|l| u16::from_str(l).ok(), |r| u8::try_from(r).ok()),
/// Some(EitherOrBoth::Both(1u16, 1u8))
/// );
/// assert_eq!(
/// EitherOrBoth::Both("tree", 1).filter_map_any(|l| u16::from_str(l).ok(), |r| u8::try_from(r).ok()),
/// Some(EitherOrBoth::Right(1u8))
/// );
/// assert_eq!(
/// EitherOrBoth::Both("1", 1024).filter_map_any(|l| u16::from_str(l).ok(), |r| u8::try_from(r).ok()),
/// Some(EitherOrBoth::Left(1u16))
/// );
/// assert_eq!(
/// EitherOrBoth::Both("tree", 1024).filter_map_any(|l| u16::from_str(l).ok(), |r| u8::try_from(r).ok()),
/// None
/// );
/// ```
pub fn filter_map_any<F, G, L, R>(self, f: F, g: G) -> Option<EitherOrBoth<L, R>>
where
F: FnOnce(A) -> Option<L>,
G: FnOnce(B) -> Option<R>,
{
match self {
Left(a) => f(a).map(Left),
Right(b) => g(b).map(Right),
Both(a, b) => match (f(a), g(b)) {
(None, None) => None,
(Some(l), None) => Some(Left(l)),
(None, Some(r)) => Some(Right(r)),
(Some(l), Some(r)) => Some(Both(l, r)),
}
}
}

/// Apply the function `f` on the value `a` in `Left(a)` or `Both(a, _)` variants if it is
/// present.
pub fn left_and_then<F, L>(self, f: F) -> EitherOrBoth<L, B>
Expand Down Expand Up @@ -482,6 +716,33 @@ impl<T> EitherOrBoth<T, T> {
Both(a, b) => f(a, b),
}
}

/// Equivalent to `map_any`, but using the same function for both values when
/// they have the same type.
pub fn map<F, U>(self, f: F) -> EitherOrBoth<U, U>
where
F: Fn(T) -> U
{
self.map_any(&f, &f)
}

/// Equivalent to `filter_any`, but using the same function for both values when
/// they have the same type.
pub fn filter<F>(self, f: F) -> Option<EitherOrBoth<T, T>>
where
F: Fn(&T) -> bool
{
self.filter_any(&f, &f)
}

/// Equivalent to `filter_map_any`, but using the same function for both values when
/// they have the same type.
pub fn filter_map<F, U>(self, f: F) -> Option<EitherOrBoth<U, U>>
where
F: Fn(T) -> Option<U>
{
self.filter_map_any(&f, &f)
}
}

impl<A, B> Into<Option<Either<A, B>>> for EitherOrBoth<A, B> {
Expand Down