packages feed

national-australia-bank 0.0.10 → 0.0.11

raw patch · 3 files changed

+50/−16 lines, 3 files

Files

changelog.md view
@@ -1,3 +1,7 @@+0.0.11++* fix cumulative reporting functions+ 0.0.10  * split out modules
national-australia-bank.cabal view
@@ -1,5 +1,5 @@ name:                 national-australia-bank-version:              0.0.10+version:              0.0.11 synopsis:             Functions for National Australia Bank transactions description:          Parsing, Processing and other functions for National Australia Bank transactions license:              BSD3
src/Data/Bank/NationalAustraliaBank/Report.hs view
@@ -1,7 +1,15 @@ {-# OPTIONS_GHC -Wall #-}+{-# LANGUAGE LambdaCase #-} -module Data.Bank.NationalAustraliaBank.Report where+module Data.Bank.NationalAustraliaBank.Report(+  cumulativeSumTransactions+, cumulativeDays+, cumulativeDays'+, cumulativePerDay+, cumulativePerDay'+) where +import Control.Lens ( view, over, Field1(_1), Field2(_2) ) import Data.Bank.NationalAustraliaBank.Transaction     ( Transaction,       Amount,@@ -10,42 +18,64 @@       realAmount,       date',       amount' )+import Data.Bool ( bool )+import Data.List ( sortOn ) import Data.Ratio ( Ratio ) +withMinimumDate ::+  (Date -> [a])+  -> [Transaction]+  -> [a]+withMinimumDate _ [] =+  []+withMinimumDate f ts@(_:_) =+  f (minimum (fmap date' ts))++sortedByDateTransactions ::+  ([(Integer, Transaction)] -> [(Integer, x)])+  -> [Transaction]+  -> [x]+sortedByDateTransactions f ts =+  let ts' =+        sortOn (date' . view _2) (zip [0..] ts)+      r =+        sortOn (view _1) (f ts')+  in  fmap (view _2) r+ cumulativeSumTransactions ::   [Transaction]   -> [Amount]-cumulativeSumTransactions [] =-  []-cumulativeSumTransactions (h:t) =-  scanl (\a t' -> amount' t' <> a) (amount' h) t+cumulativeSumTransactions =+  sortedByDateTransactions+    (\case+        [] ->+          []+        (n, h):t ->+          scanl (\(_, a) -> over _2 (\t' -> amount' t' <> a)) (n, amount' h) t+    )  cumulativeDays ::   [Transaction]   -> [Integer]-cumulativeDays [] =-  []-cumulativeDays r@(h:_) =-  cumulativeDays' (date' h) r+cumulativeDays ts =+  withMinimumDate (`cumulativeDays'` ts) ts  cumulativeDays' ::   Date   -> [Transaction]   -> [Integer] cumulativeDays' d =-  fmap (\t -> d `diffDates` date' t + 1)+  sortedByDateTransactions (fmap (fmap (\t -> date' t `diffDates` d + 1)))  cumulativePerDay ::   [Transaction]   -> [Ratio Integer]-cumulativePerDay [] =-  []-cumulativePerDay r@(h:_) =-  cumulativePerDay' (date' h) r+cumulativePerDay ts =+  withMinimumDate (`cumulativePerDay'` ts) ts  cumulativePerDay' ::   Date   -> [Transaction]   -> [Ratio Integer] cumulativePerDay' dt txs =-  zipWith (\a i -> realAmount a / fromInteger i) (cumulativeSumTransactions txs) (cumulativeDays' dt txs)+  zipWith (\a i -> bool (realAmount a / fromInteger i) 0 (i == 0)) (cumulativeSumTransactions txs) (cumulativeDays' dt txs)