blob: 371fba2b4e9f2bd9cd821c9caefa77c3a244d05b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
module Statistics
( compute
) where
import qualified Data.List as L
import qualified Data.Map as M
import qualified Data.Time.Calendar as Calendar
import Common.Model (Payment (..), PaymentStats)
compute :: [Payment] -> PaymentStats
compute payments =
M.toList $ foldl
(\m p -> M.alter (alter p) (startOfMonth $ _payment_date p) m)
M.empty
payments
where
initMonthStats =
M.fromList
. map (\category -> (category, 0))
. L.nub
$ map _payment_category payments
alter p Nothing = Just (addPayment p initMonthStats)
alter p (Just monthStats) = Just (addPayment p monthStats)
addPayment p monthStats = M.adjust ((+) (_payment_cost p)) (_payment_category p) monthStats
startOfMonth day =
let (y, m, _) = Calendar.toGregorian day
in Calendar.fromGregorian y m 1
|