2.0
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
withQuantity.for
Quantity.product
with the newQuantity.times
Quantity.scaleBy
withQuantity.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
anddivideBy
are used to multiply (scale) or divide aQuantity
by a plainInt
orFloat
times
,over
andover_
are used to work with quantities that are products of other quantities, likeArea
orVolume
per
,at
,at_
andfor
are used to work with rates of change, likeSpeed
or orCurrent
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 asProduct units units
Cubed units
has been redefined asProduct (Product units units) units
Joules
has been redefined asProduct Newtons Meters
Newtons
has been changed fromRate Joules Meters
toProduct Kilograms MetersPerSecondSquared
New Quantity
functions
A few new functions have been added to the Quantity
module:
Quantity.lessThanOrEqualTo
andQuantity.greaterThanOrEqualTo
to go with the existingQuantity.lessThan
andQuantity.greaterThan
Quantity.interpolateFrom
to interpolate from one value to anotherQuantity.midpoint
to find the midpoint between two quantitiesQuantity.sortBy
to sort an arbitrary list of values by a derived quantity