From 0638f65020a40273eaaa4451a38ac859783bc4b8210c5305382374c2f8eaec3c Mon Sep 17 00:00:00 2001 From: Nicholas Johnson Date: Fri, 19 Apr 2024 00:00:00 +0000 Subject: Replace for loop with iterator --- src/hitomezashi.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/hitomezashi.rs b/src/hitomezashi.rs index 24a59be..88ba387 100644 --- a/src/hitomezashi.rs +++ b/src/hitomezashi.rs @@ -47,6 +47,12 @@ pub fn hitomezashi(width: usize, height: usize, skew: Option) { col_bits.push(brn.sample(&mut rng)); } + let mut alt_bits: Vec = Vec::with_capacity(width - 1); + + for col in 0..width { + alt_bits.push(col % 2 == 1); + } + // each new row of the pattern depends on the bits directly above it let mut above_bits: Vec = Vec::with_capacity(width); @@ -65,13 +71,17 @@ pub fn hitomezashi(width: usize, height: usize, skew: Option) { // height-1 because the first row has already been printed for row in 0..(height - 1) { - for col in 0..width { - /* each square in each successive row is derived from the square above it. the row bits - * represent whether there's a stitch between the two squares. if there's a stitch, the - * squares are different, otherwise they are the same. */ - above_bits[col] = above_bits[col] ^ (row_bits[row] ^ (col % 2 == 1)); - print_square(above_bits[col]); - } + /* each square in each successive row is derived from the square above it. the row bits + * represent whether there's a stitch between the two squares. if there's a stitch, the + * squares are different, otherwise they are the same. */ + above_bits + .iter_mut() + .zip(alt_bits.iter()) + .for_each(|(x1, &x2)| { + *x1 ^= x2 ^ row_bits[row]; + print_square(*x1); + }); + println!(); } } -- cgit v1.2.3