]> Git — Sourcephile - comptalang.git/blob - lib/Hcompta/Account.hs
Ajout : Hcompta.Chart.
[comptalang.git] / lib / Hcompta / Account.hs
1 {-# LANGUAGE DeriveDataTypeable #-}
2 module Hcompta.Account where
3
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(..))
15
16 import qualified Hcompta.Lib.NonEmpty as NonEmpty
17 import Hcompta.Lib.Regex (Regex)
18 import Hcompta.Lib.TreeMap (TreeMap)
19
20 -- * The 'Account' type
21
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
27
28 -- | Return the 'Account' formed by the given 'Account_Section' and 'Account_Section's.
29 account :: Account_Section -> [Account_Section] -> Account
30 account = (:|)
31
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
35
36 -- | Return the given 'Account' without its last 'Account_Section' if any.
37 ascending :: Account -> Maybe Account
38 ascending = NonEmpty.ascending
39
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
44 where
45 go :: [Account_Section] -> [Account_Section] -> (Account -> a -> a) -> a -> a
46 go s [] f acc = f (n0:|s) acc
47 go s (n:ns) f acc =
48 go ((Data.List.++) s [n]) ns f (f (n0:|s) acc)
49
50 -- | Return an 'Account' from the given list.
51 from_List :: [Account_Section] -> Account
52 from_List = NonEmpty.fromList
53
54 -- * The 'Joker' type
55
56 type Joker
57 = [Joker_Section]
58 data Joker_Section
59 = Joker_Any
60 | Joker_Section Account_Section
61 deriving (Data, Eq, Show, Typeable)
62
63 -- * Type 'Pattern'
64
65 data Pattern
66 = Pattern_Exact Account
67 | Pattern_Joker Joker
68 | Pattern_Regex Regex
69 deriving (Show, Typeable)
70