aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Malodivo/Budget.hs31
1 files changed, 8 insertions, 23 deletions
diff --git a/lib/Malodivo/Budget.hs b/lib/Malodivo/Budget.hs
index 8d70629..935f86c 100644
--- a/lib/Malodivo/Budget.hs
+++ b/lib/Malodivo/Budget.hs
@@ -3,12 +3,8 @@
Budget planning in the Kingdom of Malodivo.
-}
-{-# LANGUAGE DeriveAnyClass #-}
-{-# LANGUAGE DeriveGeneric #-}
-
module Malodivo.Budget
- ( BillBudget
- , billsByMinistry
+ ( billsByMinistry
, manyToOne
, manyToMany
, manyToManyLimited
@@ -16,24 +12,13 @@ module Malodivo.Budget
) where
import Control.Arrow ((&&&))
-import GHC.Generics (Generic)
-import Data.Aeson (ToJSON)
import qualified Data.HashMap.Strict as HM
import qualified Malodivo.Types.Bill as B
import qualified Malodivo.Types.District as D
import qualified Malodivo.Types.Ministry as M
--- | This is output type. It describes contribution of each district into a bill.
-data BillBudget = BillBudget
- { bill :: B.Bill
- , districts :: [D.DistrictInfo]
- } deriving (Generic, ToJSON)
-
-zipBills :: [B.Bill] -> [D.DistrictFunds] -> [BillBudget]
-zipBills = zipWith $ \b df -> BillBudget {bill = b, districts = D.df2di df}
-
{-|
Group bills by ministry.
@@ -195,12 +180,12 @@ Districts funding multiple bills. No constraints.
manyToMany ::
D.DistrictFunds -- ^ Amounts of available funds per district.
-> [B.Bill] -- ^ Bills requiring funding.
- -> [BillBudget] -- ^ Contribution of each district to each bill.
-manyToMany df bills = zipBills bills allocated
+ -> [(B.Bill, D.DistrictFunds)] -- ^ Contribution of each district to each bill.
+manyToMany funds bills = zipWith (\b df -> (b, df)) bills allocated
where
billAmounts = B.amount <$> bills
- fundsTotal = sum $ HM.elems df
- allocated = flip fundRaising df <$> normalizeDown fundsTotal billAmounts
+ fundsTotal = sum $ HM.elems funds
+ allocated = flip fundRaising funds <$> normalizeDown fundsTotal billAmounts
{-|
Districts funding multiple bills. But each ministry may have been limited
@@ -226,8 +211,8 @@ in the amount of funds it can get.
>>> let allBills = scienceBills ++ welfareBills
>>> let funds = HM.fromList [(D.Lakos, 1000)]
->>> let findBill b = find (\bb -> B.name (bill bb) == B.name b)
->>> let amounts budget = map (B.amount . bill . fromJust) $ map (\b -> findBill b budget) allBills
+>>> let findBill b = find (\bb -> B.name (fst bb) == B.name b)
+>>> let amounts budget = map (B.amount . fst . fromJust) $ map (\b -> findBill b budget) allBills
If all bills of the specific ministry can't get enough funds due to limits imposed on the ministry,
@@ -247,7 +232,7 @@ manyToManyLimited ::
D.DistrictFunds -- ^ Amounts of available funds per district.
-> M.MinistryLimits -- ^ Maximum funds ministries can get.
-> [B.Bill] -- ^ Bills requiring funding.
- -> [BillBudget] -- ^ Contribution of each district to each bill.
+ -> [(B.Bill, D.DistrictFunds)] -- ^ Contribution of each district to each bill.
manyToManyLimited df ml bills = manyToMany df . concat $ HM.elems byMinistry'
where
byMinistry = billsByMinistry bills