[][src]Crate monadic

Haskell style block macros "mdo" where monad sources should be expressions of type instances of IntoIterator.

Each monad expression is flat_mapped (into_iter().flat_map( lambda)) with the lambda expression having the monad result variable as argument and the rest as its body, into a lazy FlatMap expression that is also an instance of IntoIterator, and can be collected into any instance of FromIterator.

To use "mdo" (module monad) you must import the traits Bind and Monad defined there.

There are transitive implementation relations for some structures to be instances of IntoIterator:

All iterators implement IntoIterator where into_iter() returns the self iterator structure as documented

Iterator and IntoIterator trait imports are predefined

There are also Reader, Writer and State monads in their respective modules with their own macros.

use num::Integer;
use monadic::{mdo, monad::{Bind, Monad}};

   // available in examples/comprehension.rs

   let xs = mdo!{ 

           x <- 1..7;
           y <- 1..x;
           guard (&y).is_odd() ;
           let z = match x.is_even() { 
                       true => &y + 1,
                       _ => &y - 1,
                   };
           pure (x, z)
            
   }.collect::<Vec<(i32,i32)>>();
    
   println!("result: {:?}", xs); 

   // now with container and item references, available in examples/comprehension2.rs

   let ys = mdo!{ 
    
       &x <- &vec![1,2,3,4];  // with item refs (&x) in the lambda argument position
       guard x.is_odd() ;
       let z = x + 1 ;
       pure (x, z)
        
   }.collect::<Vec<(i32,i32)>>();
    
   println!("result: {:?}", ys); 

Modules

mio
monad

definition of Bind and Monad traits based monadic macro

monoid

Semigroup and Monoid to use with Writer

reader
reader_trans
state

A State monad implementation

state_trans
util
writer

A Writer monad implementation

writer_trans

Macros

mdo

macro for iterables (IntoIterator) as monads enabling monad comprehensions over iterables

rdrdo

macro for a Reader<'a, E, A> monad with a boxed (env -> a) function

rdrt_mdo

macro for a ReaderT<'a, E, M> monad transformer with a boxed (env -> m a) where M: Monad + FromIterator. It uses the type alias Env in type annotations

stdo

macro for a State<'a, S, A> monad with a boxed (s -> (a, s)) function

stt_mdo

macro for a StateT<'a, S, M, A> monad transformer with a boxed (a -> m (a, s)) where M: Monad; It uses the type alias St in type annotations.

wrdo

macro for a Writer<A, W = String> monad that holds a pair (A, Monoid)

wrt_mdo

macro for a WriterT<M, W = String> monad transformer holding a pair (M, W) where M: MPlus + FromIterator, W: Monoid; It uses the type alias Log in type annotations