Elex.Unit (Elex v0.3.0)
View SourceA unit of measure as a canonical monomial.
Evaluate returns a unit monomial only. Zero exponents are dropped. Names
are not split on trailing digits (m2 stays %{"m2" => 1}). Inspect
always formats the monomial with | and ^ (m^2, m | s, m | s^2).
A single exponent-1 symbol prints as that symbol (N, mm).
same?/2 compares monomials. convertible?/3 takes a catalog and is true
when both units have the same dimension vector (m and km of :length).
compatible?/3 maps a unit monomial to a category formula and compares it
to a category (cm | s vs :speed).
Fields
:monomial- A map of symbols to integer exponents
Examples
%Elex.Unit{monomial: %{"m" => 1}}
%Elex.Unit{monomial: %{"m" => 1, "s" => -1}}
Summary
Functions
Returns true when unit's category formula matches category in catalog.
Returns true when both units have the same dimension vector in catalog.
Builds a unit from a monomial.
Builds a unit from a formula string or monomial map.
Same as new/1, but returns the unit or raises ArgumentError.
Types
Functions
@spec compatible?(t(), atom(), Elex.Units.Catalog.t()) :: boolean()
Returns true when unit's category formula matches category in catalog.
Each symbol in the unit monomial is mapped to its category and exponents
are combined. The result is compared to the category's formula (or dim).
cm | s is compatible with :speed even if cm/s is not a registered unit.
@spec convertible?(t(), t(), Elex.Units.Catalog.t()) :: boolean()
Returns true when both units have the same dimension vector in catalog.
Unknown symbols are not convertible.
Builds a unit from a monomial.
An empty monomial is invalid (nil).
Builds a unit from a formula string or monomial map.
An empty monomial (%{} or only zero exponents) is an error.
Returns
{:ok, unit}- A parsed unit{:error, String.t()}- The formula could not be parsed
Same as new/1, but returns the unit or raises ArgumentError.