diff options
author | Igor Pashev <pashev.igor@gmail.com> | 2017-06-26 09:09:20 +0300 |
---|---|---|
committer | Igor Pashev <pashev.igor@gmail.com> | 2017-06-26 09:09:20 +0300 |
commit | c82a64701ff64283e95efbbd6df614cd1e775e52 (patch) | |
tree | 771140e4a38137796e1bbd9717274b86691a2ba7 /cmd | |
parent | 6f18125faaf7afa6d543b074eca65836a13a372b (diff) | |
download | molodivo-c82a64701ff64283e95efbbd6df614cd1e775e52.tar.gz |
Refactor
Mainly because further development will not preserve
bill order in lists and we'll need to maintain mapping
internally.
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/Main.hs | 6 | ||||
-rw-r--r-- | cmd/Main/DistrictInfo.hs | 34 | ||||
-rw-r--r-- | cmd/Main/Input.hs | 9 | ||||
-rw-r--r-- | cmd/Main/Output.hs | 30 |
4 files changed, 7 insertions, 72 deletions
diff --git a/cmd/Main.hs b/cmd/Main.hs index da28a98..69ce114 100644 --- a/cmd/Main.hs +++ b/cmd/Main.hs @@ -18,9 +18,8 @@ import Text.InterpolatedString.Perl6 (qc) import Malodivo.Budget (manyToMany) -import Main.DistrictInfo (di2df) +import Malodivo.Types.District (di2df) import qualified Main.Input as I -import qualified Main.Output as O import Paths_malodivo (version) -- from cabal usageHelp :: String @@ -48,10 +47,9 @@ process = do Left err -> die err Right si -> let allBills = I.bills si - billFunds = manyToMany suppliedFunds allBills suppliedFunds = di2df $ I.districts si in do when (HM.null suppliedFunds) $ die "We needs at least one district" - L.putStr . encode $ O.zipBills allBills billFunds + L.putStr . encode $ manyToMany suppliedFunds allBills main :: IO () main = do diff --git a/cmd/Main/DistrictInfo.hs b/cmd/Main/DistrictInfo.hs deleted file mode 100644 index f4f9ff1..0000000 --- a/cmd/Main/DistrictInfo.hs +++ /dev/null @@ -1,34 +0,0 @@ -{-# 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 -<https://github.com/bos/aeson/issues/79>. 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 index 63e5942..9c59283 100644 --- a/cmd/Main/Input.hs +++ b/cmd/Main/Input.hs @@ -13,10 +13,11 @@ import GHC.Generics (Generic) import Data.Aeson (FromJSON) import Malodivo.Types.Bill (Bill) - -import Main.DistrictInfo (DistrictInfo) +import Malodivo.Types.District (DistrictInfo) +import Malodivo.Types.Ministry (MinistryInfo) data Input = Input - { bills :: [Bill] - , districts :: [DistrictInfo] + { bills :: [Bill] -- ^ bills requiring funding. + , districts :: [DistrictInfo] -- ^ funds districts can provide. + , ministry :: Maybe [MinistryInfo] -- ^ maximum funds ministries can get. } deriving (Generic, FromJSON) diff --git a/cmd/Main/Output.hs b/cmd/Main/Output.hs deleted file mode 100644 index 1024ecd..0000000 --- a/cmd/Main/Output.hs +++ /dev/null @@ -1,30 +0,0 @@ -{- -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} |