4 releases
Uses old Rust 2015
0.0.4 | Feb 23, 2015 |
---|---|
0.0.3 | Jan 12, 2015 |
0.0.2 | Jan 4, 2015 |
0.0.1 | Dec 8, 2014 |
#9 in #dimensional
Used in sandvox
5KB
123 lines
nd_iter
Create 2 and 3 dimensional iterations
Have you ever wanted to iterate through 2 or 3 dimensional space? You are probably stuck writing something like this:
for x in range(0, n) {
for y in range(0, n) {
for z in range(0, n) {
do_something(x, y, z);
}
}
}
This is a common enough pattern for me that I wrote iter_nd
to squash
iterators.
With iter_nd
you could write the above code as:
for (x, y, z) in iter_3d(range(0,n), range(0,n), range(0,n)) {
do_something(x, y, z);
}
Way simpler, way flatter, and most importantly you can easily define a function
that returns the result of your call to iter_3d
, something that would be
way harder to do in the nested for-loop example.
Example
extern crate nd_iter;
use nd_iter::iter_2d;
use nd_iter::iter_3d;
use std::iter::range_inclusive;
fn main() {
for (x, y) in iter_2d(range(0u, 2), range_inclusive(2u, 5)) {
println!("{}, {}", x, y);
}
for (x, y, z) in iter_3d(range(0u, 2), range(2u, 4), range(0u, 2)) {
println!("{}, {}, {}", x, y, z);
}
}