4 releases (2 breaking)
Uses old Rust 2015
0.3.0 | Oct 20, 2017 |
---|---|
0.2.0 | Jul 9, 2017 |
0.1.1 | Jul 7, 2017 |
0.1.0 | Jun 26, 2017 |
#2255 in Rust patterns
17KB
208 lines
README
This is a Rust macro that implements for comprehensions similar to Scala's.
Example:
let l = map_for!{
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 (|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.