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 lazyclass;
pub mod optionclass;
pub mod resultclass;
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>
where
Self::F<'a, A>: 'a,
{
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>
where
Self::F<'a, A>: 'a,
{
Box::new(|| ffa()())
}

View File

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

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>
where
Self::F<'a, A>: 'a,
{
ffa
}