diff options
author | Igor Pashev <pashev.igor@gmail.com> | 2017-06-27 06:50:32 +0300 |
---|---|---|
committer | Igor Pashev <pashev.igor@gmail.com> | 2017-06-27 06:50:32 +0300 |
commit | f3e509381908a4a1444a205734b00ab13e3316f7 (patch) | |
tree | 1e47e98fc91323d7d1ddc9d320329cb50096a397 /lib | |
parent | c589d99a57949fb02aa6f35f38adc043dcaabf9e (diff) | |
download | molodivo-f3e509381908a4a1444a205734b00ab13e3316f7.tar.gz |
Implement manyToMany with manyToOne
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Malodivo/Budget.hs | 16 |
1 files 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 |