reductor

Crates.ioreductor
lib.rsreductor
version0.0.12
sourcesrc
created_at2021-08-20 16:24:37.121206
updated_at2024-03-26 22:47:20.954889
descriptionGeneric abstractions for combining and nesting reduction patterns for iterables.
homepage
repositoryhttps://github.com/yotamofek/reductor/
max_upload_size
id440051
size26,492
Yotam Ofek (yotamofek)

documentation

README

reductor

Generic abstractions for combining and nesting reduction patterns for iterables.

Docs: https//docs.rs/reductor

Before & After:

Before

fn process_samples(
    samples: &[i32],
    scale: &[i32],
    upper_limit: i32
) {
    let mut sum = 0;
    let mut min = None;
    let mut max = None;

    for (sample, scale) in samples.iter().zip(scale) {
        let scaled = sample * scale;

        if scaled <= upper_limit {
            continue;
        }

        sum += scaled;
        min = Some(match min {
            Some(min) => scaled.min(min),
            None => scaled,
        });
        max = Some(match max {
            Some(max) => scaled.max(max),
            None => scaled,
        });
    }

    // ...
}

After

use reductor::{Reduce, Reductors, Sum, Min, Max};

fn process_samples(
    samples: &[i32],
    scale: &[i32],
    upper_limit: i32
) {
    let Reductors((Sum::<i32>(sum), Min::<Option<i32>>(min), Max::<Option<i32>>(max))) = samples
        .iter()
        .zip(scale)
        .map(|(sample, scale)| sample * scale)
        .filter(|&scaled| scaled <= upper_limit)
        .reduce_with();

    // ...
}
Commit count: 31

cargo fmt