1 {-# LANGUAGE DeriveDataTypeable #-}
2 module Hcompta.Account where
4 import Data.Data (Data)
5 import Data.Eq (Eq(..))
6 import qualified Data.Foldable
7 import qualified Data.List
8 import Data.List.NonEmpty (NonEmpty(..))
9 import qualified Data.List.NonEmpty as NonEmpty
10 import Data.Maybe (Maybe(..))
11 import Data.Text (Text)
12 import Data.Typeable (Typeable)
13 import Prelude (($), Integer, Num(..), const)
14 import Text.Show (Show(..))
16 import qualified Hcompta.Lib.NonEmpty as NonEmpty
17 import Hcompta.Lib.Regex (Regex)
18 import Hcompta.Lib.TreeMap (TreeMap)
20 -- * The 'Account' type
22 -- | An 'Account' is a non-empty list of 'Account_Section'.
23 type Account = Account_Path
24 type Account_Path = NonEmpty Account_Section
25 type Account_Section = Text
26 type Account_Tree x = TreeMap Account_Section x
28 -- | Return the 'Account' formed by the given 'Account_Section' and 'Account_Section's.
29 account :: Account_Section -> [Account_Section] -> Account
32 -- | Return the number of 'Account_Section's in the given 'Account'.
33 depth :: Account -> Integer
34 depth = Data.Foldable.foldl' (\d -> const $ d + 1) 0
36 -- | Return the given 'Account' without its last 'Account_Section' if any.
37 ascending :: Account -> Maybe Account
38 ascending = NonEmpty.ascending
40 -- | Apply the given function to all the prefixes
41 -- of the given 'Account' (including itself).
42 foldr :: Account -> (Account -> a -> a) -> a -> a
43 foldr (n0:|n0s) = go [] n0s
45 go :: [Account_Section] -> [Account_Section] -> (Account -> a -> a) -> a -> a
46 go s [] f acc = f (n0:|s) acc
48 go ((Data.List.++) s [n]) ns f (f (n0:|s) acc)
50 -- | Return an 'Account' from the given list.
51 from_List :: [Account_Section] -> Account
52 from_List = NonEmpty.fromList
60 | Joker_Section Account_Section
61 deriving (Data, Eq, Show, Typeable)
66 = Pattern_Exact Account
69 deriving (Show, Typeable)