comprehension

Crates.iocomprehension
lib.rscomprehension
version0.2.0
sourcesrc
created_at2020-02-22 22:20:29.608613
updated_at2020-08-03 10:36:44.565756
descriptionIterator comprehension in Rust
homepage
repositoryhttps://github.com/tanakh/comprehension-rs
max_upload_size
id211579
size12,456
Hideyuki Tanaka (tanakh)

documentation

README

comprehension-rs

Iterator comprehension in Rust

Usage

The syntax is derived from Haskell's list comprehension. This library use iterators instead of lists.

// this returns the iterator generates `[0, 1, 4, ..., 81]`
iter![x * x; x <- 0..10];

You can also use patterns in generators,

iter![x * y; (x, y) <- vec![(1, 1), (2, 3), (4, 5)]];
// => [1, 6, 20]

filtering values,

iter![(i, j); i <- 1.., j <- 1..i, gcd(i, j) == 1].take(10)
// => [(1, 1), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5)]

and let bindings.

iter![(i, j); i <- 1.., let k = i * i, j <- 1..=k].take(10);
// => [(1, 1), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5)]

Some useful variants are provided.

vect! returns Vec:

// just same as iter![].collect::<Vec<_>>()
vect![x * x; x <- 0..10];

sum! return sum of iterator:

let t = sum![x; x <- 1..=10]; // => 55

// same as this:
// let t = iter![x; x <- 1..=10].sum()

// but this does not compiles (need type annotation).
let t = iter![x; x <- 1..=10].sum::<i32>()

// `sum!` can infer the return type, so it has non-trivial functionality.

Also has product! macro:

let t = product![x; <- 1..=10]; // => 3628800
Commit count: 6

cargo fmt