Crate map_for [] [src]

This is a Rust macro that implements for comprehensions similar to Scala's. This allows to chain calls to map, flat_map and filter in a very clean and concise manner.

Example:

let l = map_for!{
   move;
   x <- 0..10;
   y = x/2;
   if (y%2) == 0;
   z <- 0..1;
   => y+z };

Will be expanded to:

let l = (0..10).map (move |x| { let y = x / 2; (x, y) })
   .filter (move |params| { let (x, y) = *params; (y%2) == 0 })
   .flat_map (move |params| {
      let (x, y) = params;
      (0..1).map (move |z| { y + z }) });

Macros

map_for

Scala-like for comprehension similar to those described in https://stackoverflow.com/questions/3754089/scala-for-comprehension#3754568 The main difference is that since rust does not have partial functions, we do not support general patterns in the left-hand sides, but only single identifiers and list of identifiers (that will match a tuple).

Traits

Filter

This trait creates a filter method for Option so that it can be filtered on when used in the map_for macro.

FlatMap

This trait creates a flat_map method for Option (equivalent to Option::and_then) so that it can be used in the map_for macro.