Crate rsmonad_macros
source ·Expand description
Provides the monad! {...}
macro, which parses (1) a data structure definition, (2) a function bind
, and (3) a function consume
, and implements the most idiomatic macro available as Rust continues to evolve.
Use
// use rsmonad::prelude::*; // <-- In your code, use this; here, though, we redefine a simpler `Maybe`, so we can't import everything
use rsmonad::prelude::{Monad, monad};
monad! {
/// Encodes the possibility of failure.
enum ExampleMaybe<A> {
EgNothing,
EgJust(A),
}
fn bind(self, f) {
match self {
EgNothing => EgNothing,
EgJust(b) => f(b),
}
}
fn consume(a) {
EgJust(a)
}
}
fn could_overflow(x: u8) -> ExampleMaybe<u8> {
x.checked_add(1).map_or(EgNothing, EgJust)
}
assert_eq!(
EgNothing >> could_overflow,
EgNothing
);
assert_eq!(
EgJust(1) >> could_overflow,
EgJust(2)
);
assert_eq!(
EgJust(255) >> could_overflow,
EgNothing
);
Macros
- Write the boilerplate for a monad given the minimal definition.