massex v0.1.0 Massex View Source

Defines a whole value pattern container for masses, and utility methods for working with them to improve handling within your applications.

Link to this section Summary

Functions

Returns a Massex with the arithmetical absolute of the amount

Adds two Massex structs together, returning a Massex

Compares two Massex structs, returning 0 on equality, 1 if left is greater than right, or -1 if left is less than right

Divides a Massex by the provided denominator

Returns true if two Massex represent the same amount of mass

Multiplies a Massex by the provided amount

Returns true if the amount of a Massex is less than zero

Builds a Massex struct from an amount and unit

Returns true if the amount of a Massex is more than zero

Subtracts one Massex struct from another, returning a Massex

Returns the Decimal amount backing the Massex

Returns true if the amount of a Massex is zero

Link to this section Types

Specs

t() :: %Massex{amount: Decimal.t(), unit: atom()}

Link to this section Functions

Specs

abs(t()) :: t()

Returns a Massex with the arithmetical absolute of the amount

Specs

add(t(), t() | number() | String.t()) :: t() | {:error, :invalid_amount}

Adds two Massex structs together, returning a Massex

Examples

iex> left = Massex.new(10, :gram)
iex> right = Massex.new(20, :gram)
iex> Massex.add(left, right)
%Massex{unit: :gram, amount: Decimal.new(30)}
Link to this function

compare(massex1, massex2)

View Source

Specs

compare(t(), t()) :: integer()

Compares two Massex structs, returning 0 on equality, 1 if left is greater than right, or -1 if left is less than right

Examples

iex> less = Massex.new(10, :gram)
iex> more = Massex.new(20, :gram)
iex> Massex.compare(less, less)
0
iex> Massex.compare(less, more)
-1
iex> Massex.compare(more, less)
1
Link to this function

divide(massex, denominator)

View Source

Specs

divide(t(), number()) :: t()

Divides a Massex by the provided denominator

Examples

iex> base = Massex.new(10, :gram)
iex> Massex.divide(base, 2)
%Massex{amount: Decimal.new(5), unit: :gram}
Link to this function

equals?(massex1, massex2)

View Source

Specs

equals?(t(), t()) :: boolean()

Returns true if two Massex represent the same amount of mass

Examples

iex> left = Massex.new(10, :gram)
iex> right = Massex.new(10, :gram)
iex> Massex.equals?(left, right)
true

Specs

multiply(t(), number()) :: t()

Multiplies a Massex by the provided amount

Examples

iex> mass = Massex.new(10, :gram)
iex> Massex.multiply(mass, 10)
%Massex{amount: Decimal.new(100), unit: :gram}

Specs

negative?(t()) :: boolean()

Returns true if the amount of a Massex is less than zero

Examples

iex> Massex.negative?(Massex.new(-10, :gram))
true
iex> Massex.negative?(Massex.new(10, :gram))
false

Specs

new(number() | Decimal.t() | String.t(), atom() | String.t()) :: t() | :error

Builds a Massex struct from an amount and unit

Examples

iex> Massex.new(10, :gram)
%Massex{amount: Decimal.new(10), unit: :gram}

Specs

positive?(t()) :: boolean()

Returns true if the amount of a Massex is more than zero

Examples

iex> Massex.positive?(Massex.new(-10, :gram))
false
iex> Massex.positive?(Massex.new(10, :gram))
true

Specs

subtract(t(), t() | number() | String.t()) :: t() | {:error, :invalid_amount}

Subtracts one Massex struct from another, returning a Massex

Examples

iex> left = Massex.new(20, :gram)
iex> right = Massex.new(10, :gram)
iex> Massex.subtract(left, right)
%Massex{unit: :gram, amount: Decimal.new(10)}
iex> Massex.subtract(left, 10)
%Massex{unit: :gram, amount: Decimal.new(10)}

Specs

to_decimal(t()) :: Decimal.t()

Returns the Decimal amount backing the Massex

Examples

iex> mass = Massex.new(20, :gram)
iex> Massex.to_decimal(mass)
Decimal.new(20)

Specs

zero?(t()) :: boolean()

Returns true if the amount of a Massex is zero

Examples

iex> Massex.zero?(Massex.new(-10, :gram))
false
iex> Massex.zero?(Massex.new(0, :gram))
true