iter-comprehensions 0.3.0

A library for iterator comprehensions
Documentation

iter-comprehensions

rs-graph crate iter-comprehensions docs

Introduction

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

  1. iter! for an iterator
  2. vec! for constructing vectors
  3. sum! for computing the sum of some values
  4. product! for computing the product of some values

The macro iter! can be used to generate a sequence of elements using generating sequences and conditional filters.

iter!(EXPR; VAR in RANGE, COND, ...)

where EXPR the generator expression, VAR is a variable RANGE a some sequence, e.g. 0..n (in fact, everything implementing IntoIterator). COND is an arbitrary boolean expression. Each RANGE and COND term can use the variables declared in preceeding range expressions.

Example

The expression $\{ 5i + j : i \in \{0..4\}, j \in \{0..4\}, i < j \}$ is equivalent to the following form

use iter_comprehensions::iter;
assert_eq!(iter!(4*i + j; i in 0..5, j in 0..5, i < j).collect::<Vec<_>>(),
           vec![1, 2, 3, 4, 6, 7, 8, 11, 12, 16]);

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 iter-comprehensions = "^0.3.0" into the Cargo.toml of your project.

Documentation

See docs.rs.

Example

Download

Source code of latest tagged version: iter-comprehensions-v0.3.0.tar.gz

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