rust-monadic
- A monad bloc macro based on Bind and Monad as supertraits of IntoIterator (iterables)
- A Reader monad bloc macro
- 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 end with a monadic expr.
monadic_expression
- 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, unlike Haskell.
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 Reader monad macro rdrdo!
A Reader monad adaptation macro example
//! examples/reader1
//!
//! You must specify in a type restriction the type of the environment of the Reader bloc
//!
//! `local` can be used as a function or as a method
use ;
use partial;
use HashMap;
type Env = ;
Execution:
)
The Writer monad macro wrdo!
A Writer monad adaptation macro example with String as logger, from examples/writer1.rs
//! 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< _, String > = wrdo!{...}`
//!
//! `censor(), listen() and listens()` can be used as functions or as methods of a Writer bloc
use ;
use concat_string_str;
use partial;
Exec:
) )
Example 2 with Vec as logger from examples/writer2.rs
//! examples/writer2.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< _, Vec<_> > = wrdo!{...}`
//!
//! `censor(), listen() and listens()` can be used as functions or as methods of a Writer bloc
use ;
use concat_vec_array;
use partial;
) )
The State monad macro stdo!
A State monad adaptation macro example from examples/state1.rs
//! examples/state1.rs
//!
//! You may specify in a type restriction the type of the State bloc
//! or apply it directly to an initial_state without the type restriction
use ;
) )
Changes:
v 0. 4.0:
- renamed writer function
censor_do
as censor - added writer function listen() and listens()
- renamed local_do() as local()
- removed intoiter module as it duplicates functionality without added applicability, use module monad's
mdo
macro instead
v. 0.3.14: added writer function censor_do
v. 0.3.13: added reader function local_do
v. 0.3.12: example reader1 simplification.
v. 0.3.11: suppressed the form "&v <- ..." from Writer and State monads.
v. 0.3.10: Added the Reader macro. It runs good over clonable environments e.g. HashMap. The State macro has been updated, using a non static lifetime for the boxed closure
v. 0.3.9: Added (<-) rhs pure
.