Skip to content

Multiply Ratio and Angle #315

@jacg

Description

@jacg

Ratio and Angle cannot be multiplied, because of mismatching Kinds.

Why would I want this to work? Imagine a weighted-mean function, here hard-wired for Length:

pub fn mean_weighted_length(data: &[Length], weights: &[Ratio]) -> Option<Length> {
    let total_weight: Ratio = weights.iter().cloned().sum();
    data.iter()
        .cloned()
        .zip(weights.iter().cloned())
        .map(   |(d, w)| d * w)
        .reduce(| a, b | a + b)
        .map(|x| x / total_weight)
}

I should be able to write such a function for any Quantity, not just Length. And it fails to compile if Length is replaced with Angle:

pub fn mean_weighted_angle(data: &[Angle], weights: &[Ratio]) -> Option<Angle> {
    let total_weight: Ratio = weights.iter().cloned().sum();
    data.iter()
        .cloned()
        .zip(weights.iter().cloned())
        .map(   |(d, w)| d * w)
        .reduce(| a, b | a + b)
        .map(|x| x / total_weight)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions