From f3e509381908a4a1444a205734b00ab13e3316f7 Mon Sep 17 00:00:00 2001 From: Igor Pashev Date: Tue, 27 Jun 2017 06:50:32 +0300 Subject: Implement manyToMany with manyToOne --- lib/Malodivo/Budget.hs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/lib/Malodivo/Budget.hs b/lib/Malodivo/Budget.hs index 935f86c..3a68fdf 100644 --- a/lib/Malodivo/Budget.hs +++ b/lib/Malodivo/Budget.hs @@ -172,7 +172,9 @@ manyToOne :: D.DistrictFunds -- ^ Amounts of available funds per district. -> B.Bill -- ^ A bill requiring funding. -> D.DistrictFunds -- ^ Contribution of each district. -manyToOne df b = fundRaising (B.amount b) df +manyToOne df b = HM.fromList $ zip ds (normalizeDown (B.amount b) fs) + where + (ds, fs) = unzip $ HM.toList df {-| Districts funding multiple bills. No constraints. @@ -183,9 +185,8 @@ manyToMany :: -> [(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 funds - allocated = flip fundRaising funds <$> normalizeDown fundsTotal billAmounts + allocated = manyToOne funds <$> normalizeBills fundsTotal bills {-| Districts funding multiple bills. But each ministry may have been limited @@ -241,12 +242,3 @@ manyToManyLimited df ml bills = manyToMany df . concat $ HM.elems byMinistry' case HM.lookup m ml of Nothing -> bs Just l -> normalizeBills l bs - --- | Helper function to maintain DRY and backward compatibility. -fundRaising :: - Integer -- ^ Amount to be raised. - -> D.DistrictFunds -- ^ Amounts of available funds per district. - -> D.DistrictFunds -- ^ Contribution of each district. -fundRaising needed df = HM.fromList $ zip ds (normalizeDown needed fs) - where - (ds, fs) = unzip $ HM.toList df -- cgit v1.2.3