Mizur v1.0.1 Mizur
Mizur is a tool to simplify the management, conversions and mapping of units.
The manipulation of units of measurement try (at best) to be typesafe.
Summary
Types
This type represents a results of a comparison
This type represents a unit of measure (defined with using Mizur.System)
This type represents a value wrapped in a metric system
Functions
Makes the addition between two typed_value
of the same metric system.
The return value will have the subtype of the left typed_value
Comparison between two typed_value
of the same metric system
Divides a typed_value
by a number
. The subtype of the return value
will be the subtype of the left typed_value
Returns true
if two yped_values
have the same numeric
value (in the same metric system). false
otherwise
Converts a typed_value
to another subtype of its metric system
Reformulation of Mizur.in_system/2
Checks if a typed_value
is included in a system
Checks if a typed_value
is included in a metric_type
Applies a function to the numeric value of a typed_value
and re-packs
the result of the function in the same subtype
Applies a function to the two numeric values of two typed_values
in
the same metric system, and re-packages the result
of the function in a typed_value
of the subtype of the left typed_values
Returns the biggest typed_value
Returns the smallest typed_value
Multiplies a typed_value
by a number
. The subtype of the return value
will be the subtype of the left typed_value
Checks if two typed_value
has the same system
Checks if two typed_value
has the same type
Returns true
if two yped_values
have the same numeric
value (in the same type). false
otherwise
Makes the subtraction between two typed_value
of the same metric system.
The return value will have the subtype of the left typed_value
Retrieves the system of a typed_value
Retrieves the type of a typed_value
Retrieves the wrapped numeric value in a typed_value
Types
This type represents a results of a comparison
This type represents a unit of measure (defined with using Mizur.System)
This type represents a value wrapped in a metric system
Functions
Makes the addition between two typed_value
of the same metric system.
The return value will have the subtype of the left typed_value
.
Warning: Arithmetic operations are not allowed for extensive system
iex> a = MizurTest.Distance.cm(12)
...> b = MizurTest.Distance.m(2)
...> Mizur.add(a, b)
MizurTest.Distance.cm(212)
Comparison between two typed_value
of the same metric system.
The function returns:
:eq
forequals
:lt
if the left-values is lower than the right-values:gt
if the left-values is greater than the right-values
For example:
iex> x = MizurTest.Distance.m(1)
...> y = MizurTest.Distance.cm(100)
...> Mizur.compare(x, with: y)
:eq
Divides a typed_value
by a number
. The subtype of the return value
will be the subtype of the left typed_value
.
Warning: Arithmetic operations are not allowed for extensive system
iex> a = MizurTest.Distance.cm(12)
...> Mizur.div(a, 2)
MizurTest.Distance.cm(6.0)
Returns true
if two yped_values
have the same numeric
value (in the same metric system). false
otherwise.
iex> a = MizurTest.Distance.cm(100)
...> b = MizurTest.Distance.m(1)
...> c = MizurTest.Temperature.celsius(1)
...> {Mizur.equals(a, b), Mizur.equals(b, c)}
{true, false}
Converts a typed_value
to another subtype of its metric system.
For example:
iex> x = MizurTest.Distance.cm(120)
...> Mizur.from(x, to: MizurTest.Distance.m)
{MizurTest.Distance.m, 1.2}
Reformulation of Mizur.in_system/2
For example:
iex> x = MizurTest.Distance.cm(12)
...> Mizur.in?(x, system: MizurTest.Temperature)
false
Checks if a typed_value
is included in a system.
For example:
iex> x = MizurTest.Distance.cm(12)
...> Mizur.in_system?(x, MizurTest.Distance)
true
Checks if a typed_value
is included in a metric_type
.
For example:
iex> x = MizurTest.Distance.cm(12)
...> Mizur.in_type?(x, MizurTest.Distance.cm)
true
Applies a function to the numeric value of a typed_value
and re-packs
the result of the function in the same subtype.
For example:
iex> MizurTest.Distance.km(120)
...> |> Mizur.map(fn(x) -> x * 2 end)
{MizurTest.Distance.km, 240.0}
Applies a function to the two numeric values of two typed_values
in
the same metric system, and re-packages the result
of the function in a typed_value
of the subtype of the left typed_values
.
For example:
iex> a = MizurTest.Distance.m(100)
...> b = MizurTest.Distance.km(2)
...> Mizur.map2(a, b, &(&1 * &2))
{MizurTest.Distance.m, 200000.0}
Returns the biggest typed_value
.
iex> Mizur.max(MizurTest.Distance.cm(12), MizurTest.Distance.m(1))
MizurTest.Distance.m(1)
Returns the smallest typed_value
.
iex> Mizur.min(MizurTest.Distance.cm(12), MizurTest.Distance.m(1))
MizurTest.Distance.cm(12)
Multiplies a typed_value
by a number
. The subtype of the return value
will be the subtype of the left typed_value
.
Warning: Arithmetic operations are not allowed for extensive system
iex> a = MizurTest.Distance.cm(12)
...> Mizur.mult(a, 100)
MizurTest.Distance.cm(1200)
Checks if two typed_value
has the same system.
For example:
iex> x = MizurTest.Distance.cm(12)
...> y = MizurTest.Distance.km(100)
...> Mizur.same_system?(x, y)
true
Checks if two typed_value
has the same type.
For example:
iex> x = MizurTest.Distance.cm(12)
...> y = MizurTest.Distance.cm(100)
...> Mizur.same_type?(x, y)
true
Returns true
if two yped_values
have the same numeric
value (in the same type). false
otherwise.
iex> a = MizurTest.Distance.cm(100)
...> b = MizurTest.Distance.cm(100)
...> c = MizurTest.Distance.m(1)
...> {Mizur.strict_equals(a, b), Mizur.strict_equals(b, c)}
{true, false}
Makes the subtraction between two typed_value
of the same metric system.
The return value will have the subtype of the left typed_value
.
Warning: Arithmetic operations are not allowed for extensive system
iex> a = MizurTest.Distance.cm(12)
...> b = MizurTest.Distance.m(2)
...> Mizur.sub(b, a)
MizurTest.Distance.m(1.88)
Retrieves the system of a typed_value
.
For example:
iex> x = MizurTest.Distance.cm(12)
...> Mizur.system_of(x)
MizurTest.Distance
Retrieves the type of a typed_value
.
For example:
iex> x = MizurTest.Distance.cm(12)
...> Mizur.type_of(x)
MizurTest.Distance.cm
Retrieves the wrapped numeric value in a typed_value
.
For example:
iex> x = MizurTest.Distance.cm(12)
...> Mizur.unwrap(x)
12.0