### 10 releases (1 stable)

1.0.0 | Jan 17, 2024 |
---|---|

0.5.2 | Jan 11, 2024 |

0.5.0 | May 29, 2022 |

0.4.2 | May 4, 2021 |

0.1.0 | Mar 30, 2019 |

#**381** in Rust patterns

**MIT**license

19KB

224 lines

# iter-comprehensions

## Introduction

provides a few macros implementing iterator and vector
comprehensions for `iter-comprehensions`

.`Rust`

for generating a sequence of index tuples`comprehension!`

for generating a sequence of expressions`map!`

for constructing vectors`vec!`

for computing the sum of some values`sum!`

for computing the product of some values`product!`

The macro

can be used to generate a sequence of elements using
generating sequences and conditional filters.`comprehension!`

`comprehension!``(`i1 `in` `RANGE1``,` `COND1``,` `...``,` ik `in` RANGEk`)`

where RANGE* are iterators (in fact, everything implementing

)
and each COND* is a boolean condition. Each `IntoIterator`

and `RANGE`

term can use
the variables declared in preceeding range expressions.`COND`

The macro

adds an additional expression that computes a value
depending on the indices:`map!`

`map!``(`i1 `in` `RANGE1``,` `COND1``,` `...``,` ik `in` RANGEk`,` `EXPR``)`
`map!``(``EXPR``;` i1 `in` `RANGE1``,` `COND1``,` `...``,` ik `in` RANGEk`)`

### Example

The expression

is
equivalent to the following form`{`5i `+` j`:` i ∈ `{``0``,`…`,``4``}``,` j ∈ `{``0``,`…`,``4``}``,` i `<` j `}`

`use` `iter_comprehensions``::`map`;`
`assert_eq!``(``map!``(``5``*`i `+` j`;` i `in` `0``..``5``,` j `in` `0``..``5``,` i `<` j`)``.``collect``::``<``Vec``<``_``>``>``(``)``,`
`vec!``[``1``,` `2``,` `3``,` `4``,` `7``,` `8``,` `9``,` `13``,` `14``,` `19``]``)``;`

The analogous syntax can be used to create vectors:

`use` `iter_comprehensions``::`vec`;`
`assert_eq!``(``vec!``[`i`;` i `in` `0``..``5``]``,` `vec!``[``0``,``1``,``2``,``3``,``4``]``)``;`
`assert_eq!``(``vec!``[``(`i`,`j`)``;` i `in` `0``..``3``,` j `in` `0``..``3``,` i `<` j`]``,`
`vec!``[``(``0``,``1``)``,` `(``0``,``2``)``,` `(``1``,``2``)``]``)``;`

Computing a sum of values:

`use` `iter_comprehensions``::``{`sum`,` vec`}``;`
`assert_eq!``(``sum!``(`i`;` i `in` `1``..``10``,` i `%` `2` `==` `1``)``,` `25``)``;`
`let` S `=` `vec!``[`i`;` i `in` `1``..``10``]``;`
`assert_eq!``(``sum!``(`i `in` S`,` i `%` `2` `==` `1``,` i`)``,` `25``)``;`

Computing a product of values:

`use` `iter_comprehensions``::`product`;`
`assert_eq!``(``product!``(`i`;` i `in` `1``..``=``5``)``,` `120``)``;`
`assert_eq!``(``product!``(`i `in` `1``..``=``5``,` i`)``,` `120``)``;`

## Author

Frank Fischer frank-fischer@shadow-soft.de

## Installation

Put the requirement

into the `iter -comprehensions = "^1.0.0"`

`Cargo.toml`

of
your project.## Documentation

See docs.rs.

## Example

## Download

Source code of latest tagged version: iter-comprehensions-v1.0.0.tar.gz

Source code of trunk: iter-comprehensions-trunk.tar.gz