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 /lib/Malodivo/Types | |
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 'lib/Malodivo/Types')
-rw-r--r-- | lib/Malodivo/Types/District.hs | 34 | ||||
-rw-r--r-- | lib/Malodivo/Types/Ministry.hs | 25 |
2 files changed, 51 insertions, 8 deletions
diff --git a/lib/Malodivo/Types/District.hs b/lib/Malodivo/Types/District.hs index 058377b..2e78634 100644 --- a/lib/Malodivo/Types/District.hs +++ b/lib/Malodivo/Types/District.hs @@ -18,11 +18,17 @@ Just [Lakos] module Malodivo.Types.District ( District(..) + , DistrictFunds + , DistrictInfo(..) + , di2df + , df2di ) where +import Control.Arrow ((&&&)) import GHC.Generics (Generic) import Data.Aeson (FromJSON, ToJSON) +import qualified Data.HashMap.Strict as HM import Data.Hashable (Hashable) -- | District of the Kindom of Malodivo. @@ -30,10 +36,24 @@ data District = Palolene | SouthernPalolene | Lakos - deriving ( Eq - , Hashable - , Show - , Generic - , FromJSON - , ToJSON - ) + deriving (Eq, Hashable, Show, Generic, FromJSON, ToJSON) + +-- | Convenient type. +type DistrictFunds = HM.HashMap District Integer + +{-| +We use this data type instead of 'DistrictFunds' for JSON, +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. +-} +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/lib/Malodivo/Types/Ministry.hs b/lib/Malodivo/Types/Ministry.hs index c3e315b..f3878d1 100644 --- a/lib/Malodivo/Types/Ministry.hs +++ b/lib/Malodivo/Types/Ministry.hs @@ -18,15 +18,38 @@ Just [Science] module Malodivo.Types.Ministry ( Ministry(..) + , MinistryInfo + , MinistryLimits + , mi2ml ) where +import Control.Arrow ((&&&)) import GHC.Generics (Generic) import Data.Aeson (FromJSON, ToJSON) +import qualified Data.HashMap.Strict as HM +import Data.Hashable (Hashable) -- | Ministry of the Kingdom of Malodivo. data Ministry = Defense | Science | Welfare - deriving (Show, Generic, FromJSON, ToJSON) + deriving (Eq, Hashable, Show, Generic, FromJSON, ToJSON) + +-- | Convenient type. Describes maximum money a ministry can get. +type MinistryLimits = HM.HashMap Ministry Integer + +{-| +We use this data type instead of 'MinistryLimits' for JSON, +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. +-} +data MinistryInfo = MinistryInfo + { name :: Ministry + , amount :: Integer + } deriving (Generic, FromJSON) + +mi2ml :: [MinistryInfo] -> MinistryLimits +mi2ml = HM.fromListWith (+) . map (name &&& amount) |