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

comparison_result()
comparison_result() :: :eq | :lt | :gt

This type represents a results of a comparison

metric_type()
metric_type() :: {module, atom, boolean, (number -> float), (number -> float)}

This type represents a unit of measure (defined with using Mizur.System)

typed_value()
typed_value() :: {metric_type, float}

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)
compare(arg, list)
compare(typed_value, [{:with, typed_value}]) :: comparison_result

Comparison between two typed_value of the same metric system.

The function returns:

  • :eq for equals
  • :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
div(a, b)
div(typed_value, number) :: typed_value

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)
equals(a, b)
equals(typed_value, typed_value) :: boolean

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}
from(arg, list)
from(typed_value, [{:to, metric_type}]) :: typed_value

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}
in?(value, list)

Reformulation of Mizur.in_system/2

For example:

iex> x = MizurTest.Distance.cm(12)
...> Mizur.in?(x, system: MizurTest.Temperature)
false
in_system?(arg1, m)
in_system?(typed_value, module) :: boolean

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
in_type?(arg1, t)
in_type?(typed_value, metric_type) :: boolean

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
map(arg, f)
map(typed_value, (number -> number)) :: typed_value

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}
map2(arg, elt2, f)
map2(typed_value, typed_value, (number, number -> number)) :: typed_value

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)
mult(a, b)
mult(typed_value, number) :: typed_value

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)
same_system?(arg1, arg2)
same_system?(typed_value, typed_value) :: boolean

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
same_type?(arg1, arg2)
same_type?(typed_value, typed_value) :: boolean

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
strict_equals(a, b)
strict_equals(typed_value, typed_value) :: boolean

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)
system_of(arg)
system_of(typed_value) :: module

Retrieves the system of a typed_value.

For example:

iex> x = MizurTest.Distance.cm(12)
...> Mizur.system_of(x)
MizurTest.Distance
type_of(arg)
type_of(typed_value) :: metric_type

Retrieves the type of a typed_value.

For example:

iex> x = MizurTest.Distance.cm(12)
...> Mizur.type_of(x)
MizurTest.Distance.cm
unwrap(arg)
unwrap(typed_value) :: float

Retrieves the wrapped numeric value in a typed_value.

For example:

iex> x = MizurTest.Distance.cm(12)
...> Mizur.unwrap(x)
12.0