ResultClass + better lifetimes + less matches

This commit is contained in:
AF 2023-03-12 20:47:49 +00:00
parent b264389cbf
commit 00c5239ce5
6 changed files with 75 additions and 34 deletions

View File

@ -1,4 +1,5 @@
pub mod futureclass; pub mod futureclass;
pub mod lazyclass; pub mod lazyclass;
pub mod optionclass; pub mod optionclass;
pub mod resultclass;
pub mod soloclass; pub mod soloclass;

View File

@ -71,8 +71,6 @@ impl Monad for FutureClass {
} }
fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A> fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A>
where
Self::F<'a, A>: 'a,
{ {
Box::pin(async { ffa.await.await }) Box::pin(async { ffa.await.await })
} }

View File

@ -66,8 +66,6 @@ impl Monad for LazyClass {
} }
fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A> fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A>
where
Self::F<'a, A>: 'a,
{ {
Box::new(|| ffa()()) Box::new(|| ffa()())
} }

View File

@ -21,10 +21,7 @@ impl ApplicativeSeq for OptionClass {
ff: Self::F<'a, F>, ff: Self::F<'a, F>,
fa: Self::F<'a, A>, fa: Self::F<'a, A>,
) -> Self::F<'a, B> { ) -> Self::F<'a, B> {
match (ff, fa) { Self::pure(ff?(fa?))
(Some(f), Some(a)) => Some(f(a)),
_ => None,
}
} }
} }
@ -34,10 +31,7 @@ impl ApplicativeLA2 for OptionClass {
fa: Self::F<'a, A>, fa: Self::F<'a, A>,
fb: Self::F<'a, B>, fb: Self::F<'a, B>,
) -> Self::F<'a, C> { ) -> Self::F<'a, C> {
match (fa, fb) { Self::pure(f(fa?, fb?))
(Some(a), Some(b)) => Some(f(a, b)),
_ => None,
}
} }
} }
@ -47,17 +41,13 @@ impl Applicative for OptionClass {
} }
fn discard_first<'a, A: 'a, B: 'a>(fa: Self::F<'a, A>, fb: Self::F<'a, B>) -> Self::F<'a, B> { fn discard_first<'a, A: 'a, B: 'a>(fa: Self::F<'a, A>, fb: Self::F<'a, B>) -> Self::F<'a, B> {
match fa { fa?;
Some(_) => fb, fb
None => None,
}
} }
fn discard_second<'a, A: 'a, B: 'a>(fa: Self::F<'a, A>, fb: Self::F<'a, B>) -> Self::F<'a, A> { fn discard_second<'a, A: 'a, B: 'a>(fa: Self::F<'a, A>, fb: Self::F<'a, B>) -> Self::F<'a, A> {
match fb { fb?;
Some(_) => fa, fa
None => None,
}
} }
} }
@ -66,20 +56,11 @@ impl Monad for OptionClass {
fa: Self::F<'a, A>, fa: Self::F<'a, A>,
f: F, f: F,
) -> Self::F<'a, B> { ) -> Self::F<'a, B> {
match fa { f(fa?)
Some(a) => f(a),
None => None,
}
} }
fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A> fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A> {
where ffa?
Self::F<'a, A>: 'a,
{
match ffa {
Some(Some(a)) => Some(a),
_ => None,
}
} }
} }

View File

@ -0,0 +1,65 @@
use crate::func::*;
pub struct ResultClass<E>(E);
impl<E> WeakFunctor for ResultClass<E> {
type F<'a, A> = Result<A, E>;
}
impl<E> Functor for ResultClass<E> {
fn fmap<'a, A: 'a, B: 'a, F: 'a + FnOnce(A) -> B>(f: F, fa: Self::F<'a, A>) -> Self::F<'a, B> {
fa.map(f)
}
fn replace<'a, A: 'a, B: 'a>(fa: Self::F<'a, A>, b: B) -> Self::F<'a, B> {
fa.map(|_| b)
}
}
impl<E> ApplicativeSeq for ResultClass<E> {
fn seq<'a, A: 'a, B: 'a, F: 'a + FnOnce(A) -> B>(
ff: Self::F<'a, F>,
fa: Self::F<'a, A>,
) -> Self::F<'a, B> {
Self::pure(ff?(fa?))
}
}
impl<E> ApplicativeLA2 for ResultClass<E> {
fn la2<'a, A: 'a, B: 'a, C: 'a, F: 'a + FnOnce(A, B) -> C>(
f: F,
fa: Self::F<'a, A>,
fb: Self::F<'a, B>,
) -> Self::F<'a, C> {
Self::pure(f(fa?, fb?))
}
}
impl<E> Applicative for ResultClass<E> {
fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A> {
Ok(a)
}
fn discard_first<'a, A: 'a, B: 'a>(fa: Self::F<'a, A>, fb: Self::F<'a, B>) -> Self::F<'a, B> {
fa?;
fb
}
fn discard_second<'a, A: 'a, B: 'a>(fa: Self::F<'a, A>, fb: Self::F<'a, B>) -> Self::F<'a, A> {
fb?;
fa
}
}
impl<E> Monad for ResultClass<E> {
fn bind<'a, A: 'a, B: 'a, F: 'a + FnOnce(A) -> Self::F<'a, B>>(
fa: Self::F<'a, A>,
f: F,
) -> Self::F<'a, B> {
f(fa?)
}
fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A> {
ffa?
}
}

View File

@ -65,8 +65,6 @@ impl Monad for SoloClass {
} }
fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A> fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A>
where
Self::F<'a, A>: 'a,
{ {
ffa ffa
} }