62 lines
1.7 KiB
Haskell
62 lines
1.7 KiB
Haskell
module ShortShow (shortShow, ShortShow, ShortString (..)) where
|
|
|
|
import Geometry
|
|
import Numeric
|
|
|
|
class ShortShow a where
|
|
shortShow :: a -> String
|
|
|
|
instance ShortShow a => ShortShow (V2 a) where
|
|
shortShow (V2 x y) = shortShow x ++ "#" ++ shortShow y
|
|
|
|
instance ShortShow a => ShortShow (V3 a) where
|
|
shortShow (V3 x y z) = shortShow x ++ "#" ++ shortShow y
|
|
++ "#" ++ shortShow z
|
|
|
|
newtype ShortString = SString String
|
|
|
|
instance ShortShow ShortString where
|
|
shortShow (SString x) = show x
|
|
|
|
instance ShortShow Float where
|
|
shortShow x = showFFloat (Just 2) x ""
|
|
|
|
instance ShortShow Bool where
|
|
shortShow True = "T"
|
|
shortShow False = "F"
|
|
|
|
instance ShortShow a => ShortShow (Maybe a) where
|
|
shortShow (Just x) = "J#" <> shortShow x
|
|
shortShow Nothing = "NTHNG"
|
|
|
|
instance ShortShow Int where
|
|
shortShow x
|
|
| x < k' = show x
|
|
| x < m = fdiv x k "K"
|
|
| x < g = fdiv x m "M"
|
|
| x < t = fdiv x g "G"
|
|
| x < p = fdiv x t "T"
|
|
| otherwise = show x ++ "P"
|
|
|
|
fdiv :: Int -> Int -> String -> String
|
|
fdiv x y s = removeDot (take 3 (show ((fromIntegral x / fromIntegral y) :: Float))) ++ s
|
|
|
|
removeDot :: String -> String
|
|
removeDot [a, b, '.'] = [a, b]
|
|
removeDot xs = xs
|
|
|
|
k, k', m, g, t, p :: Int
|
|
k = 10 ^ (3 :: Int)
|
|
k' = 10 ^ (4 :: Int) -- this allows showing up to 9999 directly
|
|
m = 10 ^ (6 :: Int)
|
|
g = 10 ^ (9 :: Int)
|
|
t = 10 ^ (12 :: Int)
|
|
p = 10 ^ (15 :: Int)
|
|
|
|
instance (ShortShow a, ShortShow b) => ShortShow (a, b) where
|
|
shortShow (a, b) = '(' : shortShow a ++ "," ++ shortShow b ++ ")"
|
|
|
|
instance (ShortShow a, ShortShow b, ShortShow c) => ShortShow (a, b, c) where
|
|
shortShow (a, b, c)
|
|
= '(' : shortShow a ++ "," ++ shortShow b ++ "," ++ shortShow c ++ ")"
|