aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor Pashev <pashev.igor@gmail.com>2017-06-27 07:02:26 +0300
committerIgor Pashev <pashev.igor@gmail.com>2017-06-27 07:02:26 +0300
commit9e9f8e11bb2728db084038c1a34272931435c30f (patch)
tree2ab68ff8432f2184b1d8d0f53092d9438472f1e4
parentf3e509381908a4a1444a205734b00ab13e3316f7 (diff)
downloadmolodivo-9e9f8e11bb2728db084038c1a34272931435c30f.tar.gz
Work with empty structures
-rw-r--r--cmd/Main.hs7
-rw-r--r--lib/Malodivo/Budget.hs30
2 files changed, 32 insertions, 5 deletions
diff --git a/cmd/Main.hs b/cmd/Main.hs
index 7af6a83..dbe6dac 100644
--- a/cmd/Main.hs
+++ b/cmd/Main.hs
@@ -4,7 +4,6 @@ module Main
( main
) where
-import Control.Monad (when)
import Data.Maybe (fromMaybe)
import Data.Version (showVersion)
import System.Environment (getArgs)
@@ -12,7 +11,6 @@ import System.Exit (die)
import Data.Aeson (eitherDecode, encode)
import qualified Data.ByteString.Lazy as L
-import qualified Data.HashMap.Strict as HM
import System.Console.Docopt.NoTH
(isPresent, longOption, parseArgsOrExit, parseUsageOrExit, usage)
import Text.InterpolatedString.Perl6 (qc)
@@ -52,9 +50,8 @@ process = do
let allBills = I.bills si
suppliedFunds = di2df $ I.districts si
ministryLimits = mi2ml $ fromMaybe [] (I.ministries si)
- in do when (HM.null suppliedFunds) $ die "We needs at least one district"
- L.putStr . encode . O.encode $
- manyToManyLimited suppliedFunds ministryLimits allBills
+ in L.putStr . encode . O.encode $
+ manyToManyLimited suppliedFunds ministryLimits allBills
main :: IO ()
main = do
diff --git a/lib/Malodivo/Budget.hs b/lib/Malodivo/Budget.hs
index 3a68fdf..c063b2a 100644
--- a/lib/Malodivo/Budget.hs
+++ b/lib/Malodivo/Budget.hs
@@ -167,6 +167,12 @@ that some districts can pay their shares and others can't:
>>> manyToOne low medium == low
True
+
+It works without any districts specified:
+
+>>> manyToOne HM.empty medium
+fromList []
+
-}
manyToOne ::
D.DistrictFunds -- ^ Amounts of available funds per district.
@@ -178,6 +184,30 @@ manyToOne df b = HM.fromList $ zip ds (normalizeDown (B.amount b) fs)
{-|
Districts funding multiple bills. No constraints.
+
+>>> :set -XOverloadedStrings
+>>> 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
+
+>>> let scienceA = B.Bill { B.amount = 10, B.name = "Science A", B.ministry = M.Science }
+>>> let welfareA = B.Bill { B.amount = 100, B.name = "Welfare A", B.ministry = M.Welfare }
+
+
+It works without any districts specified:
+
+>>> snd . head $ manyToMany HM.empty [scienceA]
+fromList []
+
+
+It works without bills:
+
+>>> manyToMany HM.empty []
+[]
+>>> let funds = HM.fromList [(D.Palolene, 100), (D.Lakos, 200)]
+>>> manyToMany funds []
+[]
-}
manyToMany ::
D.DistrictFunds -- ^ Amounts of available funds per district.