rsmonad 0.2.4

Haskell-style monads for idiomatic Rust.
Documentation
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

//! Applicative trait.

use crate::prelude::*;

/// Apply a function wrapped in a functor to an argument wrapped in a functor.
pub trait Applicative<A: Clone>: Functor<A> {
    /// Fucking pain in the ass redundancy. This has to be in this trait to avoid potential spooky action at a distance e.g. by redefining a separate Hkt later.
    type Applicative<B: Clone>: Applicative<B, Applicative<A> = Self>;
    /// Construct an Applicative from a value.
    fn consume(a: A) -> Self;
    /// Apply a function wrapped in a functor to an argument wrapped in a functor.
    fn tie<F: FnOnce(A) -> B + Clone, B: Clone>(
        self,
        af: Self::Applicative<F>,
    ) -> Self::Applicative<B>;
}

/// Construct an Applicative from a value.
#[inline(always)]
pub fn consume<Ap: Applicative<A, Applicative<A> = Ap>, A: Clone>(a: A) -> Ap {
    Ap::consume(a)
}

/// Apply a function wrapped in a functor to an argument wrapped in a functor.
#[inline(always)]
#[must_use]
pub fn tie<Ap: Applicative<A>, A: Clone, F: FnOnce(A) -> B + Clone, B: Clone>(
    aa: Ap,
    af: Ap::Applicative<F>,
) -> Ap::Applicative<B> {
    aa.tie(af)
}