Commit 4d92937f authored by Ben Gamari's avatar Ben Gamari 🐢

Show relative changes

parent 3b0324fd
......@@ -53,6 +53,11 @@ main :: IO ()
main = do
files <- execParser $ info (helper <*> args) mempty
let filesWithFriendlyNames = friendlyNameFiles files
-- Yuck. Figure out friendly name of reference set
[ref] = [ name
| (name, fname) <- M.toList filesWithFriendlyNames
, fname == head files
]
mtrees <- mapM (fmap (MTree.toMap head . fromJust) . MTree.readFile) filesWithFriendlyNames
let aligned = alignMany mtrees
let tabulate :: (Show a, Ord a) => String -> (LabelMatcher a) -> IO ()
......@@ -63,7 +68,9 @@ main = do
[ heading
, replicate 20 '='
, ""
, render show show (maybe "-" (\x -> showGFloat (Just 3) x "")) $ toTable xs
, let showRel = (maybe "-" (showRelative $ showGFloat (Just 3)))
table = toTable $ toRelativeChange ref xs
in render show show showRel table
, ""
]
where
......@@ -122,3 +129,23 @@ toTable xs =
where
allKeys = toList $ foldMap M.keysSet xs
data Relative a = Reference a
| Relative a
| NoReference
showRelative :: (Num a, Show a) => (a -> ShowS) -> Relative a -> String
showRelative showRef (Reference n) = showRef n ""
showRelative _showRef (Relative n) = shows (100*n) "%"
showRelative _showRef NoReference = "no ref"
toRelativeChange :: (Ord r, Ord c, RealFrac a)
=> c
-> M.Map r (M.Map c a)
-> M.Map r (M.Map c (Relative a))
toRelativeChange ref xs = fmap f xs
where
f ys
| Just x0 <- M.lookup ref ys =
M.mapWithKey (\k y -> if k == ref then Reference x0 else Relative ((y-x0) / x0)) ys
| otherwise = fmap (const NoReference) ys
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment