ianmackenzie / elm-units

Simple, safe and convenient unit types and conversions for Elm
https://package.elm-lang.org/packages/ianmackenzie/elm-units/latest/
BSD 3-Clause "New" or "Revised" License
85 stars 14 forks source link

Add Product units type #22

Closed ianmackenzie closed 5 years ago

ianmackenzie commented 5 years ago

Add

type Product units1 units2 =
    Product units1 units2

Then redefine Squared and Cubed as

type alias Squared units =
    Product units units

type alias Cubed units =
    Product (Product units units) units

Redefine product as

product :
    Quantity number units1
    -> Quantity number units2
    -> Quantity number (Product units1 units2)

This will allow for

quotient : 
    Quantity Float (Product units1 units2) 
    -> Quantity number units2 
    -> Quantity number units1

and perhaps

quotient_ : 
    Quantity Float (Product units1 units2) 
    -> Quantity number units1
    -> Quantity number units2

This will be a major version change, although it shouldn't actually break any change (all existing type aliases in user code should still be valid, since the Squared type is replaced by the equivalent Squared type alias).

ianmackenzie commented 5 years ago

Tweaked API: repurpose times for products (just use at and at_ for rate multiplications) and use over for division of a product, so everything is "infix"/pipeline-friendly like most other functions in Quantity:

times: 
    Quantity number units2 
    -> Quantity number units1
    -> Quantity number (Product units1 units2)

over: 
    Quantity Float units2 
    -> Quantity Float (Product units1 units2) 
    -> Quantity Float units1

over_: 
    Quantity Float units1 
    -> Quantity Float (Product units1 units2) 
    -> Quantity Float units2

This leads to code like

force =
    mass |> Quantity.times acceleration

acceleration =
    force |> Quantity.over_ mass

mass =
    force |> Quantity.over acceleration

area = 
    width |> Quantity.times height

volume =
    width |> Quantity.times height |> Quantity.times depth

volume =
    area |> Quantity.times depth -- NOT depth times area

depth =
    volume |> Quantity.over_ area

area =
    volume |> Quantity.over depth

(This assumes that Newtons are redefined as Product Kilograms MetersPerSecondSquared instead of the current Rate Joules Meters.)

ianmackenzie commented 5 years ago

Implemented and released as part of version 2.0.