Skip to content

Generic abstractions for combining and nesting reduction patterns for iterables.

Notifications You must be signed in to change notification settings

yotamofek/reductor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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();

    // ...
}

About

Generic abstractions for combining and nesting reduction patterns for iterables.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages