MonadFailAny::map_err

This commit is contained in:
AF 2023-04-25 15:11:12 +00:00
parent 4d6cfaac1c
commit 5d604d2834
3 changed files with 31 additions and 1 deletions

View File

@ -199,7 +199,8 @@ pub trait MonadFail<E>: Monad {
/// Equivalent of Haskell's `fail`.
fn fail<'a, A: 'a>(e: E) -> Self::F<'a, A>
where
Self: 'a;
Self: 'a,
E: 'a;
}
/// Equivalent of Haskell's `Alternative`.
@ -238,6 +239,14 @@ pub trait LocalFunctor: WeakFunctor {
pub trait MonadFailAny {
/// [`MonadFail`] for a specific error type.
type W<E>: MonadFail<E>;
/// Equivalent of [`Result::map_err`].
fn map_err<'a, A: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
f: impl 'a + FnOnce(E0) -> E1,
) -> <Self::W<E1> as WeakFunctor>::F<'a, A>
where
Self: 'a;
}
/// Represents a (collection of) [Monad]\(s),

View File

@ -125,6 +125,7 @@ impl<E, U: Monad, V: MonadFail<E> + LocalFunctor> MonadFail<E> for CompositionCl
fn fail<'a, A: 'a>(e: E) -> Self::F<'a, A>
where
Self: 'a,
E: 'a,
{
U::pure(V::fail(e))
}

View File

@ -163,6 +163,16 @@ pub struct ResultFailAny;
impl MonadFailAny for ResultFailAny {
type W<E> = ResultClass<E>;
fn map_err<'a, A: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
f: impl 'a + FnOnce(E0) -> E1,
) -> <Self::W<E1> as WeakFunctor>::F<'a, A>
where
Self: 'a,
{
wa.map_err(f)
}
}
impl MonadFailOver<classes::solo::SoloClass> for ResultFailAny {
@ -191,6 +201,16 @@ pub struct ResultFailOver<T: Monad>(T);
impl<T: Monad> MonadFailAny for ResultFailOver<T> {
type W<E> = super::composition::CompositionClass<T, ResultClass<E>>;
fn map_err<'a, A: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
f: impl 'a + FnOnce(E0) -> E1,
) -> <Self::W<E1> as WeakFunctor>::F<'a, A>
where
Self: 'a,
{
T::fmap(|a| a.map_err(f), wa)
}
}
impl<T: Monad> MonadFailOver<T> for ResultFailOver<T> {