rust-monadic
- the macro mdo! based on Bind and Monad over IntoIterator (iterables)
- the macro monadic! based directly on IntoIterator methods
- the macro wrdo! based on a Writer struct type as a Writer monad
The macro mdo!
A macro to write Haskell style monadic code
for IntoIterator (iterables) as monads
Each step monad expression is flat_mapped with the rest into a lazy FlatMap expression which implements IntoIterator with lambdas as move closures capturing the environment and argument. The lambda body will be recursively parsed as monadic, and its type should also be an instance of IntoIterator.
The traits Bind and Monad are defined in module monad as supertraits of IntoIterator.
You can use:
- to return an expression value:
pure return_expresion
- to use the monad result:
v <- monadic_expression
- to ignore the monad result:
_ <- monadic_expression
- to combine monad results:
let z = expression
- to filter results:
guard boolean_expression
Note: let, within the macro, introduces an expression, not a block.
Example1: monadic comprehensions à la Haskell (file: examples/comprehension.rs)
use ;
use Integer;
Execution:
Example2: variation with references to container and lambda argument position (file: examples/comprehension2.rs)
use ;
use Integer;
Execution:
The original macro monadic!
Same functionality as mdo using IntoIterator
and Iterator
methods directly, avoiding intermixed Bind
and Monad
traits definitions.
Here is example1 using it:
use monadic;
use Integer;
The Writer monad macro wrdo!
A Writer monad adaptation macro example with String as logger, from examples/writer1.rs
//! you may set the logger type by beginning with a `tell...` function within the macro `wrdo`
//! or by declaring it as the result type
//! as in `let res : Writer<(i32,i32),String = wrdo!{...}`
use ;
use partial;
// example function for use in `censor` function through a partial partial_application
Exec:
) )
Example 2 with Vec as logger from examples/writer2.rs
use ;
use partial;
) )