Crates.io | map_for |
lib.rs | map_for |
version | 0.3.0 |
source | src |
created_at | 2017-06-26 18:04:25.109021 |
updated_at | 2017-10-20 18:02:25.82914 |
description | A Rust macro that implements for comprehensions similar to Scala's. |
homepage | https://bitbucket.org/jmb/rust-map_for |
repository | |
max_upload_size | |
id | 20800 |
size | 17,659 |
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.