rust-monadic
- A monad bloc macro based on Bind and Monad as supertraits of IntoIterator (iterables)
- A monad bloc macro based directly on IntoIterator and Iterator methods
- A Writer monad bloc macro
- A State monad bloc macro
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:
Example: console io. There is a problem capturing string variables because String does not implement Copy, but it works using variables in the same closure, just in the line that follows.
// examples/console_io.rs
use ;
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
//! where String is the default if omitted
//! as in `let res : Writer< (i32, i32), String > = wrdo!{...}`
use ;
use concat_string_str;
use partial;
Exec:
) )
Example 2 with Vec as logger from examples/writer2.rs
use ;
use concat_vec_array;
use partial;
) )
The State monad macro stdo!
A State monad adaptation macro example from examples/state1.rs
use ;
) )
Added (<-) rhs pure
.