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
- numberLength :: a -> Int
- numberLengthHex :: a -> Int
- class NumberLength a => SignedNumberLength a where
- signedNumberLength :: a -> Int
- signedNumberLengthHex :: a -> Int
- class NumberLength a => BoundedNumberLength a where
- maxNumberLength :: Proxy a -> Int
- maxNumberLengthHex :: Proxy a -> Int
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
.
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
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.