//! Functional programming utilities /// The Applicative trait provides a pure behavior, /// which can be used to create values of type f a from values of type a. pub trait Applicative { /// The Associative type acts as a (f a) wrapper for Self. type WrappedSelf; /// Applicative::pure(_) is abstraction with lifts any arbitrary type to underlying higher /// order type fn pure(v: R) -> Self::WrappedSelf; } impl Applicative for Option { type WrappedSelf = Option; fn pure(v: R) -> Self::WrappedSelf { Some(v) } } impl Applicative for Result { type WrappedSelf = Result; fn pure(v: R) -> Self::WrappedSelf { Ok(v) } } /// This function wraps the evaluated result of `f` into current context, /// based on the condition provided into the `predicate` pub fn when = W>, F>(predicate: bool, f: F) -> W where F: FnOnce() -> W, { if predicate { f() } else { W::pure(()) } }