From ebe8cba9837872de3dd611d6cd615425c51fefec Mon Sep 17 00:00:00 2001 From: Igor Pashev Date: Sat, 24 Jun 2017 13:36:19 +0300 Subject: Support many bills --- cmd/Main/DistrictInfo.hs | 34 ++++++++++++++++++++++++++++++++++ cmd/Main/Input.hs | 22 ++++++++++++++++++++++ cmd/Main/Output.hs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 cmd/Main/DistrictInfo.hs create mode 100644 cmd/Main/Input.hs create mode 100644 cmd/Main/Output.hs (limited to 'cmd/Main') diff --git a/cmd/Main/DistrictInfo.hs b/cmd/Main/DistrictInfo.hs new file mode 100644 index 0000000..f4f9ff1 --- /dev/null +++ b/cmd/Main/DistrictInfo.hs @@ -0,0 +1,34 @@ +{-# LANGUAGE DeriveAnyClass #-} +{-# LANGUAGE DeriveGeneric #-} + +module Main.DistrictInfo + ( DistrictInfo(..) + , di2df + , df2di + ) where + +import Control.Arrow ((&&&)) +import GHC.Generics (Generic) + +import Data.Aeson (FromJSON, ToJSON) +import qualified Data.HashMap.Strict as HM + +import Malodivo.Budget (DistrictFunds) +import Malodivo.Types.District (District) + +{-| +We use this data type instead of 'DistrictFunds', because +maps other than @HashMap Text _@ are hard to decode/encode see +. Anyway, we still have type-checked +typos-proof structure, and build 'DistrictFunds' out of it. +-} +data DistrictInfo = DistrictInfo + { name :: District + , amount :: Integer + } deriving (Generic, FromJSON, ToJSON) + +di2df :: [DistrictInfo] -> DistrictFunds +di2df = HM.fromListWith (+) . map (name &&& amount) + +df2di :: DistrictFunds -> [DistrictInfo] +df2di = map (\(n, a) -> DistrictInfo {name = n, amount = a}) . HM.toList diff --git a/cmd/Main/Input.hs b/cmd/Main/Input.hs new file mode 100644 index 0000000..63e5942 --- /dev/null +++ b/cmd/Main/Input.hs @@ -0,0 +1,22 @@ +{- +This modules describes input data for the command line utlity. +-} +{-# LANGUAGE DeriveAnyClass #-} +{-# LANGUAGE DeriveGeneric #-} + +module Main.Input + ( Input(..) + ) where + +import GHC.Generics (Generic) + +import Data.Aeson (FromJSON) + +import Malodivo.Types.Bill (Bill) + +import Main.DistrictInfo (DistrictInfo) + +data Input = Input + { bills :: [Bill] + , districts :: [DistrictInfo] + } deriving (Generic, FromJSON) diff --git a/cmd/Main/Output.hs b/cmd/Main/Output.hs new file mode 100644 index 0000000..1024ecd --- /dev/null +++ b/cmd/Main/Output.hs @@ -0,0 +1,30 @@ +{- +This modules describes output data for the command line utlity. +-} +{-# LANGUAGE DeriveAnyClass #-} +{-# LANGUAGE DeriveGeneric #-} + +module Main.Output + ( BillBudget(..) + , zipBills + ) where + +import GHC.Generics (Generic) + +import Data.Aeson (ToJSON) + +import Malodivo.Budget (DistrictFunds) +import Malodivo.Types.Bill (Bill) + +import Main.DistrictInfo (DistrictInfo, df2di) + +-- | This is output type. It describes contribution of each district into a bill. +data BillBudget = BillBudget + { bill :: Bill + , districts :: [DistrictInfo] + } deriving (Generic, ToJSON) + +zipBills :: [Bill] -> [DistrictFunds] -> [BillBudget] +zipBills = zipWith zipper + where + zipper b df = BillBudget {bill = b, districts = df2di df} -- cgit v1.2.3