Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
kalaninja committed Sep 1, 2023
1 parent 81cdd12 commit 6dd811e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 63 deletions.
97 changes: 54 additions & 43 deletions src/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ pub trait Apply<B>: Functor<B> + Semigroupal<B> {
/// assert_eq!(Some("2".to_string()), f.ap(Some(2)));
/// ```
fn ap<A>(self, fa: Self::Target<A>) -> Self::Target<B>
where
Self::Param: FnOnce(A) -> B;
where
Self::Param: FnOnce(A) -> B;

/// Is a binary version of [ap].
///
Expand All @@ -35,13 +35,13 @@ pub trait Apply<B>: Functor<B> + Semigroupal<B> {
/// ```
#[inline]
fn ap2<A, Z>(self, fa: Self::Target<A>, fb: Self::Target<B>) -> Self::Target<Z>
where
Self::Param: FnOnce(A, B) -> Z,
Self::Target<(Self::Param, B)>: Semigroupal<A>
+ Higher<Target<A>=Self::Target<A>>
+ Higher<Target<((Self::Param, B), A)>=Self::Target<((Self::Param, B), A)>>,
Self::Target<((Self::Param, B), A)>: Functor<Z, Target<Z>=Self::Target<Z>>,
Self: Sized,
where
Self::Param: FnOnce(A, B) -> Z,
Self::Target<(Self::Param, B)>: Semigroupal<A>
+ Higher<Target<A> = Self::Target<A>>
+ Higher<Target<((Self::Param, B), A)> = Self::Target<((Self::Param, B), A)>>,
Self::Target<((Self::Param, B), A)>: Functor<Z, Target<Z> = Self::Target<Z>>,
Self: Sized,
{
self.product(fb).product(fa).map(|((f, b), a)| f(a, b))
}
Expand All @@ -57,19 +57,27 @@ pub trait Apply<B>: Functor<B> + Semigroupal<B> {
/// assert_eq!(Some(6), f.ap3(Some(1), Some(2), Some(3)));
/// ```
#[inline]
fn ap3<A, C, Z>(self, fa: Self::Target<A>, fb: Self::Target<B>, fc: Self::Target<C>) -> Self::Target<Z>
where
Self::Param: FnOnce(A, B, C) -> Z,
Self::Target<(Self::Param, B)>: Semigroupal<A>
+ Higher<Target<A>=Self::Target<A>>
+ Higher<Target<((Self::Param, B), A)>=Self::Target<((Self::Param, B), A)>>,
Self::Target<((Self::Param, B), A)>: Semigroupal<C>
+ Higher<Target<C>=Self::Target<C>>
+ Higher<Target<(((Self::Param, B), A), C)>=Self::Target<(((Self::Param, B), A), C)>>,
Self::Target<(((Self::Param, B), A), C)>: Functor<Z, Target<Z>=Self::Target<Z>>,
Self: Sized,
fn ap3<A, C, Z>(
self,
fa: Self::Target<A>,
fb: Self::Target<B>,
fc: Self::Target<C>,
) -> Self::Target<Z>
where
Self::Param: FnOnce(A, B, C) -> Z,
Self::Target<(Self::Param, B)>: Semigroupal<A>
+ Higher<Target<A> = Self::Target<A>>
+ Higher<Target<((Self::Param, B), A)> = Self::Target<((Self::Param, B), A)>>,
Self::Target<((Self::Param, B), A)>: Semigroupal<C>
+ Higher<Target<C> = Self::Target<C>>
+ Higher<Target<(((Self::Param, B), A), C)> = Self::Target<(((Self::Param, B), A), C)>>,
Self::Target<(((Self::Param, B), A), C)>: Functor<Z, Target<Z> = Self::Target<Z>>,
Self: Sized,
{
self.product(fb).product(fa).product(fc).map(|(((f, b), a), c)| f(a, b, c))
self.product(fb)
.product(fa)
.product(fc)
.map(|(((f, b), a), c)| f(a, b, c))
}

/// Combine two effectful values into a single effectful value using a binary function.
Expand All @@ -86,10 +94,10 @@ pub trait Apply<B>: Functor<B> + Semigroupal<B> {
/// ```
#[inline]
fn map2<Z, F>(self, fb: Self::Target<B>, mut f: F) -> Self::Target<Z>
where
F: FnMut(Self::Param, B) -> Z,
Self::Target<(Self::Param, B)>: Functor<Z, Target<Z>=Self::Target<Z>>,
Self: Sized,
where
F: FnMut(Self::Param, B) -> Z,
Self::Target<(Self::Param, B)>: Functor<Z, Target<Z> = Self::Target<Z>>,
Self: Sized,
{
self.product(fb).map(|(a, b)| f(a, b))
}
Expand All @@ -109,12 +117,15 @@ pub trait Apply<B>: Functor<B> + Semigroupal<B> {
/// ```
#[inline]
fn map3<C, Z, F>(self, fb: Self::Target<B>, fc: Self::Target<C>, mut f: F) -> Self::Target<Z>
where F: FnMut(Self::Param, B, C) -> Z,
Self::Target<(Self::Param, B)>: Apply<C, Target<C>=Self::Target<C>>,
Self::Target<(Self::Param, B)>: Higher<Target<Z>=Self::Target<Z>>,
<Self::Target<(Self::Param, B)> as Higher>::Target<((Self::Param, B), C)>: Functor<Z, Target<Z>=Self::Target<Z>>,
Self: Sized, {
self.product(fb).map2(fc, |(a, b), c| { f(a, b, c) })
where
F: FnMut(Self::Param, B, C) -> Z,
Self::Target<(Self::Param, B)>: Apply<C, Target<C> = Self::Target<C>>,
Self::Target<(Self::Param, B)>: Higher<Target<Z> = Self::Target<Z>>,
<Self::Target<(Self::Param, B)> as Higher>::Target<((Self::Param, B), C)>:
Functor<Z, Target<Z> = Self::Target<Z>>,
Self: Sized,
{
self.product(fb).map2(fc, |(a, b), c| f(a, b, c))
}

/// Compose two effectful values discarding the result of the first.
Expand All @@ -131,9 +142,9 @@ pub trait Apply<B>: Functor<B> + Semigroupal<B> {
/// ```
#[inline]
fn product_r(self, fb: Self::Target<B>) -> Self::Target<B>
where
Self::Target<(Self::Param, B)>: Functor<B, Target<B>=Self::Target<B>>,
Self: Sized,
where
Self::Target<(Self::Param, B)>: Functor<B, Target<B> = Self::Target<B>>,
Self: Sized,
{
self.map2(fb, |_, b| b)
}
Expand All @@ -152,9 +163,9 @@ pub trait Apply<B>: Functor<B> + Semigroupal<B> {
/// ```
#[inline]
fn product_l(self, fb: Self::Target<B>) -> Self
where
Self::Target<(Self::Param, B)>: Functor<Self::Param, Target<Self::Param>=Self>,
Self: Higher<Target<<Self as Higher>::Param>=Self> + Sized,
where
Self::Target<(Self::Param, B)>: Functor<Self::Param, Target<Self::Param> = Self>,
Self: Higher<Target<<Self as Higher>::Param> = Self> + Sized,
{
self.map2(fb, |a, _| a)
}
Expand Down Expand Up @@ -196,8 +207,8 @@ macro_rules! apply_iter {
impl<F, B> Apply<B> for PhantomData<F> {
#[inline]
fn ap<A>(self, _fa: PhantomData<A>) -> PhantomData<B>
where
Self::Param: FnOnce(A) -> B,
where
Self::Param: FnOnce(A) -> B,
{
PhantomData
}
Expand All @@ -206,8 +217,8 @@ impl<F, B> Apply<B> for PhantomData<F> {
impl<F, B> Apply<B> for Option<F> {
#[inline]
fn ap<A>(self, fa: Option<A>) -> Option<B>
where
Self::Param: FnOnce(A) -> B,
where
Self::Param: FnOnce(A) -> B,
{
self.and_then(|f| fa.map(f))
}
Expand All @@ -216,8 +227,8 @@ impl<F, B> Apply<B> for Option<F> {
impl<F, B, E> Apply<B> for Result<F, E> {
#[inline]
fn ap<A>(self, fa: Result<A, E>) -> Result<B, E>
where
Self::Param: FnOnce(A) -> B,
where
Self::Param: FnOnce(A) -> B,
{
self.and_then(|f| fa.map(f))
}
Expand Down
4 changes: 3 additions & 1 deletion src/contravariant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ use crate::invariant::Invariant;
/// let mut f = lift_contravariant(|x: i32| x.to_string());
/// assert_eq!(PhantomData::<i32>, f(PhantomData::<String>));
/// ```
pub fn lift_contravariant<FA, B>(mut f: impl FnMut(B) -> FA::Param) -> impl FnMut(FA) -> FA::Target<B>
pub fn lift_contravariant<FA, B>(
mut f: impl FnMut(B) -> FA::Param,
) -> impl FnMut(FA) -> FA::Target<B>
where
FA: Contravariant<B>,
{
Expand Down
38 changes: 19 additions & 19 deletions src/data/validated/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,8 @@ impl<T, E> Validated<T, E> {
/// ```
#[inline]
pub fn as_deref(&self) -> Validated<&T::Target, &E>
where
T: core::ops::Deref,
where
T: core::ops::Deref,
{
self.as_ref().map(|x| x.deref())
}
Expand Down Expand Up @@ -414,8 +414,8 @@ impl<T, E> Validated<T, E> {
/// ```
#[inline]
pub fn as_deref_mut(&mut self) -> Validated<&mut T::Target, &mut E>
where
T: core::ops::DerefMut,
where
T: core::ops::DerefMut,
{
self.as_mut().map(|x| x.deref_mut())
}
Expand Down Expand Up @@ -447,8 +447,8 @@ impl<T, E> Validated<T, E> {
#[inline]
#[track_caller]
pub fn expect(self, msg: &str) -> T
where
E: core::fmt::Debug,
where
E: core::fmt::Debug,
{
match self {
Valid(x) => x,
Expand Down Expand Up @@ -492,8 +492,8 @@ impl<T, E> Validated<T, E> {
#[inline]
#[track_caller]
pub fn unwrap(self) -> T
where
E: core::fmt::Debug,
where
E: core::fmt::Debug,
{
match self {
Valid(x) => x,
Expand All @@ -519,8 +519,8 @@ impl<T, E> Validated<T, E> {
/// ```
#[inline]
pub fn unwrap_or_default(self) -> T
where
T: Default,
where
T: Default,
{
match self {
Valid(x) => x,
Expand All @@ -546,8 +546,8 @@ impl<T, E> Validated<T, E> {
#[inline]
#[track_caller]
pub fn expect_err(self, msg: &str) -> E
where
T: core::fmt::Debug,
where
T: core::fmt::Debug,
{
match self {
Valid(x) => unwrap_failed(msg, &x),
Expand Down Expand Up @@ -580,8 +580,8 @@ impl<T, E> Validated<T, E> {
#[inline]
#[track_caller]
pub fn unwrap_err(self) -> E
where
T: core::fmt::Debug,
where
T: core::fmt::Debug,
{
match self {
Valid(x) => unwrap_failed("called `Validated::unwrap_err()` on a `Valid` value", &x),
Expand Down Expand Up @@ -778,9 +778,9 @@ fn unwrap_failed(msg: &str, error: &dyn core::fmt::Debug) -> ! {
}

impl<T, E> Clone for Validated<T, E>
where
T: Clone,
E: Clone,
where
T: Clone,
E: Clone,
{
fn clone(&self) -> Self {
match self {
Expand Down Expand Up @@ -826,8 +826,8 @@ impl<A, B, E: Semigroup> Semigroupal<B> for Validated<A, E> {
impl<F, B, E: Semigroup> Apply<B> for Validated<F, E> {
#[inline]
fn ap<A>(self, fa: Validated<A, E>) -> Validated<B, E>
where
Self::Param: FnOnce(A) -> B,
where
Self::Param: FnOnce(A) -> B,
{
match (self, fa) {
(Valid(f), Valid(a)) => Valid(f(a)),
Expand Down

0 comments on commit 6dd811e

Please sign in to comment.