Skip to content

2.0

Compare
Choose a tag to compare
@ianmackenzie ianmackenzie released this 15 Jan 03:32

elm-units 2.0 is out! This release adds several new quantity types, improves support for multiplication of different types of quantities, and adds a few more useful functions to the Quantity module.

Updating code

To update code using elm-units 1.0 to 2.0, it should be sufficient to replace

  • Quantity.times with Quantity.for
  • Quantity.product with the new Quantity.times
  • Quantity.scaleBy with Quantity.multiplyBy

For example:

----- 1.0 -----

length =
    speed |> Quantity.times duration

area =
    Quantity.product length width

halfLength =
    Quantity.scaleBy 0.5 length

----- 2.0 -----

length =
    speed |> Quantity.for duration

area =
   length |> Quantity.times width

halfLength =
    Quantity.multiplyBy 0.5 length

See Improved product support below for details.

New quantity types

This release adds support for several new quantity types:

  • Volume (cubic meters) - thanks @katjam!
  • Density (kilograms per cubic meter) - thanks @katjam!
  • AngularSpeed (radians per second) - thanks @katjam!
  • AngularAcceleration (radians per second squared) - thanks @katjam!
  • Capacitance (farads) - thanks @ukarim!
  • Inductance (henries) - thanks @ukarim!
  • SubstanceAmount (moles) - thanks @ukarim!

Improved product support

Support for products of two quantities is now generalized and improved. Previously it was possible to square a quantity using Quantity.squared or multiply two quantities with the same units using Quantity.product (both of which gave you a result in Squared units), but there was no way to multiply two quantities with different units. This is now supported - for example, it is now possible to multiply an Area by a Length to get a Volume, or a Mass by an Acceleration to get a Force. It is also now possible to divide these products, for example divide a Force by a Mass to get an Acceleration.

This has led to a couple of breaking changes. First of all, the existing Quantity.times (used when working with rates of change) has been renamed to Quantity.for, and Quantity.times is now used for multiplying quantities together, replacing the old Quantity.product. For example:

area =
    width |> Quantity.times length

volume =
    area |> Quantity.times depth

force =
    mass |> Quantity.times acceleration

In addition, for consistency with divideBy, scaleBy has been renamed to multiplyBy. There are now three 'families' of multiplication/division functions for use in different contexts:

  • multiplyBy and divideBy are used to multiply (scale) or divide a Quantity by a plain Int or Float
  • times, over and over_ are used to work with quantities that are products of other quantities, like Area or Volume
  • per, at, at_ and for are used to work with rates of change, like Speed or or Current

See Multiplication and division in the README for more details.

The new products support is made possible by a new Product units1 units2 units type. This has led to the redefinition of some existing units types:

  • Squared units has been redefined as Product units units
  • Cubed units has been redefined as Product (Product units units) units
  • Joules has been redefined as Product Newtons Meters
  • Newtons has been changed from Rate Joules Meters to Product Kilograms MetersPerSecondSquared

New Quantity functions

A few new functions have been added to the Quantity module:

  • Quantity.lessThanOrEqualTo and Quantity.greaterThanOrEqualTo to go with the existing Quantity.lessThan and Quantity.greaterThan
  • Quantity.interpolateFrom to interpolate from one value to another
  • Quantity.midpoint to find the midpoint between two quantities
  • Quantity.sortBy to sort an arbitrary list of values by a derived quantity