Copyright | © 2018-2020 IOHK |
---|---|
License | Apache-2.0 |
Safe Haskell | None |
Language | Haskell2010 |
Synopsis
-
newtype
Quantity
(unit ::
Symbol
) a =
Quantity
{
- getQuantity :: a
- data Percentage
- data MkPercentageError = PercentageOutOfBoundsError
- mkPercentage :: Rational -> Either MkPercentageError Percentage
- getPercentage :: Percentage -> Rational
- clipToPercentage :: Rational -> Percentage
- complementPercentage :: Percentage -> Percentage
- percentageToDouble :: Percentage -> Double
Polymorphic Quantity
newtype Quantity (unit :: Symbol ) a Source #
Quantity (unit :: Symbol) a
is a primitive
a
multiplied by an
unit
.
Example:
Instead of providing the unit implicitly as a comment, or a part of a name
>>>
a :: Word32 -- in lovelace
we can write
>>>
a :: Quantity "lovelace" Word32
which now has a different type from
>>>
b :: Quantity "lovelace/byte" Word32
so mixing them up is more difficult.
The unit is mostly a phantom type, but it is also included in the
ToJSON
/
FromJSON
instances.
>>>
Aeson.encode $ Quantity @"lovelace" 14
{"unit":"lovelace","quantity":14}
Quantity | |
|
Instances
Percentage
data Percentage Source #
Opaque Haskell type to represent values between 0 and 100 (incl).
Instances
data MkPercentageError Source #
Instances
Eq MkPercentageError Source # | |
Defined in Data.Quantity (==) :: MkPercentageError -> MkPercentageError -> Bool Source # (/=) :: MkPercentageError -> MkPercentageError -> Bool Source # |
|
Show MkPercentageError Source # | |
Defined in Data.Quantity |
mkPercentage :: Rational -> Either MkPercentageError Percentage Source #
Safe constructor for
Percentage
Takes an input in the range [0, 1].
getPercentage :: Percentage -> Rational Source #
clipToPercentage :: Rational -> Percentage Source #
Safe way to make a
Percentage
by clipping values that are
out of bounds.
complementPercentage :: Percentage -> Percentage Source #
The complement is the amount that is missing to make it 100%.
Example: The
complementPercentage
of 0.7 is 0.3.
percentageToDouble :: Percentage -> Double Source #
Turn a
Percentage
to a
Double
(without any extra rounding.)