Copyright | (c) 2015-2016 Peter Trško |
---|---|
License | BSD3 |
Stability | experimental |
Portability | DefaultSignatures, NoImplicitPrelude |
Safe Haskell | Safe |
Language | Haskell2010 |
Polymorphic interface for getting number of digits of a number in decimal or hexadecimal representation.
- class NumberLength a where
- class NumberLength a => SignedNumberLength a where
- class NumberLength a => BoundedNumberLength a where
Documentation
class NumberLength a where Source #
Get number of digits of a number in base 10 and base 16. Note the following:
- There is no
Num
constraint, so that type wrappers aren't forced to provide instance for it. This is because there are things represented using numbers, but they aren't numbers, e.g. telephone numbers. - This type class doesn't handle signed numbers, in an intuitive way. See
also
SignedNumberLength
. - There is a special class for bounded numbers, see
BoundedNumberLength
, that provides similar functionality asBounded
, but for number of digits in a number.
numberLength :: a -> Int Source #
Get number of digits in base 10 for specified number. Note that if number is signed, then this function will return length of its absolute value.
>>>
numberLength (123 :: Int)
3>>>
numberLength (-123 :: Int)
3
See also signedNumberLength
.
numberLengthHex :: a -> Int Source #
Get number of digits in base 16 for specified number. Note that if number is signed, then this function will return length of its absolute value.
>>>
numberLengthHex (123 :: Int) -- 123 = 7b in hex
2>>>
numberLengthHex (-123 :: Int)
2
See also signedNumberLengthHex
.
NumberLength Int Source # | |
NumberLength Int8 Source # | |
NumberLength Int16 Source # | |
NumberLength Int32 Source # | |
NumberLength Int64 Source # | |
NumberLength Integer Source # | Since 0.2.0.0 |
NumberLength Natural Source # | Since 0.2.0.0 |
NumberLength Word Source # | |
NumberLength Word8 Source # | |
NumberLength Word16 Source # | |
NumberLength Word32 Source # | |
NumberLength Word64 Source # | |
class NumberLength a => SignedNumberLength a where Source #
Get number of digits of a signed number in base 10 and base 16.
signedNumberLength :: a -> Int Source #
Get number of digits in base 10 for specified number.
>>>
signedNumberLength (123 :: Int)
3>>>
signedNumberLength (-123 :: Int)
4
Default implementation provided if a
has also Num
and Ord
instances:
signedNumberLength
n = signLength +numberLength
n where signLength = if n < 0 then 1 else 0
signedNumberLength :: (Num a, Ord a) => a -> Int Source #
Get number of digits in base 10 for specified number.
>>>
signedNumberLength (123 :: Int)
3>>>
signedNumberLength (-123 :: Int)
4
Default implementation provided if a
has also Num
and Ord
instances:
signedNumberLength
n = signLength +numberLength
n where signLength = if n < 0 then 1 else 0
signedNumberLengthHex :: a -> Int Source #
Get number of digits in base 16 for specified number.
>>>
signedNumberLengthHex (123 :: Int)
2>>>
signedNumberLengthHex (-123 :: Int)
16
Negative number is shown as ones' complement, e.g. (-123 :: Int) =
ffffffffffffff85
on 64 bit platform.
class NumberLength a => BoundedNumberLength a where Source #
Get maximum number of digits of a number in base 10 and 16. Minimal number of digits is considered to be always 1, and therefore there is no method for it.
maxNumberLength :: proxy a -> Int Source #
Get maximum number of digits of a number in base 10.
maxNumberLengthHex :: proxy a -> Int Source #
Get maximum number of digits of a number in base 16.