Skip to main content

fp_library/classes/
monad.rs

1//! Monads, allowing for sequencing computations where the structure depends on previous results.
2//!
3//! ### Examples
4//!
5//! ```
6//! use fp_library::{
7//! 	brands::*,
8//! 	functions::*,
9//! };
10//!
11//! // Monad combines Pointed (pure) and Semimonad (bind)
12//! let x = pure::<OptionBrand, _>(5);
13//! let y = bind::<OptionBrand, _, _>(x, |i| pure::<OptionBrand, _>(i * 2));
14//! assert_eq!(y, Some(10));
15//! ```
16
17#[fp_macros::document_module]
18mod inner {
19	use {
20		crate::classes::*,
21		fp_macros::*,
22	};
23
24	/// A type class for monads, allowing for sequencing computations where the structure of the computation depends on the result of the previous computation.
25	///
26	/// `class (Applicative m, Semimonad m) => Monad m`
27	#[document_examples]
28	///
29	/// ```
30	/// use fp_library::{
31	/// 	brands::*,
32	/// 	functions::*,
33	/// };
34	///
35	/// // Monad combines Pointed (pure) and Semimonad (bind)
36	/// let x = pure::<OptionBrand, _>(5);
37	/// let y = bind::<OptionBrand, _, _>(x, |i| pure::<OptionBrand, _>(i * 2));
38	/// assert_eq!(y, Some(10));
39	/// ```
40	pub trait Monad: Applicative + Semimonad {}
41
42	/// Blanket implementation of [`Monad`].
43	#[document_type_parameters("The brand type.")]
44	impl<Brand> Monad for Brand where Brand: Applicative + Semimonad {}
45}
46
47pub use inner::*;