fix result discard_second
All checks were successful
buildbot/cargo fmt (1.72) Build done.
buildbot/cargo clippy (1.65) Build done.
buildbot/cargo doc (1.72) Build done.
buildbot/cargo clippy (1.72) Build done.
buildbot/cargo test (1.65) Build done.

This commit is contained in:
AF 2023-10-15 14:06:39 +00:00
parent 3a87bff32f
commit e83f170dda
3 changed files with 38 additions and 7 deletions

View File

@ -13,6 +13,7 @@
//! [`solo`]: super::solo //! [`solo`]: super::solo
use crate::func::class_prelude::*; use crate::func::class_prelude::*;
#[derive(SharedFunctorAny)] #[derive(SharedFunctorAny)]
pub struct OptionInstance; pub struct OptionInstance;
@ -79,8 +80,8 @@ impl<'a> Applicative<'a> for OptionInstance {
} }
fn discard_second<A: 'a + Send, B: 'a + Send>(fa: Self::F<A>, fb: Self::F<B>) -> Self::F<A> { fn discard_second<A: 'a + Send, B: 'a + Send>(fa: Self::F<A>, fb: Self::F<B>) -> Self::F<A> {
fb?; let a = fa?;
fa fb.map(|_| a)
} }
} }

View File

@ -99,8 +99,8 @@ impl<'a, E: 'a + Send> Applicative<'a> for ResultInstance<E> {
} }
fn discard_second<A: 'a + Send, B: 'a + Send>(fa: Self::F<A>, fb: Self::F<B>) -> Self::F<A> { fn discard_second<A: 'a + Send, B: 'a + Send>(fa: Self::F<A>, fb: Self::F<B>) -> Self::F<A> {
fb?; let a = fa?;
fa fb.map(|_| a)
} }
} }
@ -264,3 +264,33 @@ impl<'a, T: Monad<'a>> MonadFailAny<'a> for ResultFailOver<T> {
T::fmap(wa, <ResultFailAny as MonadFailAny>::rotate_out) T::fmap(wa, <ResultFailAny as MonadFailAny>::rotate_out)
} }
} }
#[cfg(test)]
mod result_tests {
use super::{test_suite, tests, ResultInstance};
type T = ResultInstance<bool>;
impl<'a> tests::Eqr<'a> for T {
fn eqr<A: 'a + Send + PartialEq + std::fmt::Debug>(
name: &'a str,
left: Self::F<A>,
right: Self::F<A>,
) -> tests::R {
tests::eqr(name, left, right)
}
}
impl<'a> test_suite::FunctorTestSuite<'a> for T {
fn sample<A: 'a + Send, F: FnMut(&'a (dyn Send + Sync + Fn(A) -> Self::F<A>))>(mut f: F) {
f(&|_| Err(false));
f(&|_| Err(true));
f(&|a| Ok(a));
}
}
#[test]
fn monad_follows_laws() {
test_suite::monad_follows_laws::<T>().unwrap();
}
}

View File

@ -220,7 +220,7 @@ pub fn fmap_can_be_expressed_via_seq<
pub fn discard_can_be_expressed_via_seq_or_la2< pub fn discard_can_be_expressed_via_seq_or_la2<
'a, 'a,
T: Applicative<'a> + Eqr<'a>, T: Applicative<'a> + Eqr<'a>,
A: 'a + Send, A: 'a + Send + Debug + PartialEq,
B: 'a + Send + Debug + PartialEq, B: 'a + Send + Debug + PartialEq,
>( >(
fa0: impl 'a + Fn() -> T::F<A>, fa0: impl 'a + Fn() -> T::F<A>,
@ -232,8 +232,8 @@ pub fn discard_can_be_expressed_via_seq_or_la2<
T::seq(T::replace(fa0(), |b| b), fb0()), T::seq(T::replace(fa0(), |b| b), fb0()),
) + T::eqr( ) + T::eqr(
"discard via la2: u <* v = liftA2 const u v", "discard via la2: u <* v = liftA2 const u v",
T::discard_second(fb0(), fa0()), T::discard_second(fa0(), fb0()),
T::la2(fb0(), fa0(), |b, _| b), T::la2(fa0(), fb0(), |a, _| a),
) )
} }