From c82a64701ff64283e95efbbd6df614cd1e775e52 Mon Sep 17 00:00:00 2001 From: Igor Pashev Date: Mon, 26 Jun 2017 09:09:20 +0300 Subject: Refactor Mainly because further development will not preserve bill order in lists and we'll need to maintain mapping internally. --- cmd/Main.hs | 6 ++---- cmd/Main/DistrictInfo.hs | 34 ---------------------------------- cmd/Main/Input.hs | 9 +++++---- cmd/Main/Output.hs | 30 ------------------------------ 4 files changed, 7 insertions(+), 72 deletions(-) delete mode 100644 cmd/Main/DistrictInfo.hs delete mode 100644 cmd/Main/Output.hs (limited to 'cmd') 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 -. 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} -- cgit v1.2.3