README

This is a Rust macro that implements for comprehensions similar to Scala's.

Example:

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

Will be expanded to:

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

Note that since Rust does not have an equivalent of Scala's partial function, the comprehensions can't do full pattern bindings and only work for single bindings (ie. v <- …) or tuple bindings (ie. (x, y) <- …).

When compared with comp, map_for is more generic: comp has specialized notation and code for Option, Iterator and Result and that's it, whereas map_for will work with any type that has map, flat_map and filter methods, or that can be extended to have them as map_for.FlatMap and map_for.Filter do for Option which only has map.

When compared with mdo, map_for has a simpler and more straightforward approach since most monadic type will already define the map, flat_map and filter methods, whereas mdo requires re-implementing a specific API.