summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregor Kleen <pngwjpgh@users.noreply.github.com>2016-07-07 23:13:59 +0200
committerGregor Kleen <pngwjpgh@users.noreply.github.com>2016-07-07 23:13:59 +0200
commit3496781f3ebad2901b60f19b388c893d8ad8aa37 (patch)
treecfb2e3d9f55b744e768ebb11e4e5a325b1c21847
parente5e9eed0ebb7f63fb9cd111495220024c350879c (diff)
download2017-01-16_17:13:37-3496781f3ebad2901b60f19b388c893d8ad8aa37.tar
2017-01-16_17:13:37-3496781f3ebad2901b60f19b388c893d8ad8aa37.tar.gz
2017-01-16_17:13:37-3496781f3ebad2901b60f19b388c893d8ad8aa37.tar.bz2
2017-01-16_17:13:37-3496781f3ebad2901b60f19b388c893d8ad8aa37.tar.xz
2017-01-16_17:13:37-3496781f3ebad2901b60f19b388c893d8ad8aa37.zip
prettier output of distributions
-rw-r--r--src/Main.hs15
1 files changed, 10 insertions, 5 deletions
diff --git a/src/Main.hs b/src/Main.hs
index 75f379b..449e21d 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -56,6 +56,7 @@ import Sequence.Formula
56import Text.Layout.Table 56import Text.Layout.Table
57 57
58import Text.Read (readMaybe) 58import Text.Read (readMaybe)
59import Text.Printf
59 60
60import Data.Text (Text) 61import Data.Text (Text)
61import qualified Data.Text as Text 62import qualified Data.Text as Text
@@ -382,8 +383,12 @@ printVal = withArg $ \formula -> withFocus $ \focusId -> do
382 where 383 where
383 outline Nothing = shellPutErrLn "No such value" 384 outline Nothing = shellPutErrLn "No such value"
384 outline (Just (Map.toList -> [(v, _)])) = shellPutStrLn $ show v 385 outline (Just (Map.toList -> [(v, _)])) = shellPutStrLn $ show v
385 outline (Just (sortBy (comparing snd) . Map.toList -> vals)) = mapM_ (shellPutStrLn . outline') vals 386 outline (Just (sortBy (comparing fst) . Map.toList -> vals)) = mapM_ (shellPutStrLn . outline') vals
386 outline' (v, prob) = pad' 3 (show $ round' 10 prob) ++ " → " ++ show v 387 where
387 round' :: RealFrac a => Float -> a -> Float 388 barLength = 100
388 round' n = (/ n) . fromIntegral . (round :: RealFrac a => a -> Integer) . (* n) . realToFrac 389 outline' (v, prob) = printf "%*s: %.2f [%-*s]"
389 pad' m ys = replicate (m - length (take m ys)) ' ' ++ ys 390 (maximum lengths) (show v)
391 (fromRational prob :: Double)
392 barLength (replicate (round $ fromInteger barLength * normalize prob) '#')
393 lengths = map (length . show . fst) vals
394 normalize p = p / maximum (map snd vals)