1 {-# LANGUAGE DeriveDataTypeable #-}
2 module Hcompta.Account where
4 import Data.Data (Data)
5 import qualified Data.List
6 import qualified Data.List.NonEmpty
7 import Data.List.NonEmpty (NonEmpty(..))
8 import Data.Semigroup ((<>))
9 import Data.Typeable (Typeable)
10 -- import qualified Text.Parsec as P
11 -- import Text.Parsec (Stream, ParsecT, (<|>), (<?>))
12 import Data.Text (Text)
14 -- import qualified Hcompta.Account.Path as Path
15 import Hcompta.Lib.Regex (Regex)
16 import qualified Hcompta.Lib.TreeMap as Lib.TreeMap
18 -- * The 'Account' type
20 -- | An 'Account' is a non-empty list of 'Name'.
21 type Account = NonEmpty Name
23 type Map x = Lib.TreeMap.TreeMap Name x
25 -- | Return the 'Account' formed by the given 'Name' and 'Name's.
26 account :: Name -> [Name] -> Account
29 -- | Return the given 'Account' without its last 'Name' if any.
30 ascending :: Account -> Maybe Account
31 ascending (_:|[]) = Nothing
32 ascending (n:|ns) = Just (n:|Data.List.init ns)
33 {-# INLINE ascending #-}
35 -- | Apply the given function to all the prefixes
36 -- of the given 'Account' (including itself).
37 foldr :: Account -> (Account -> a -> a) -> a -> a
38 foldr (n0:|n0s) = go [] n0s
40 go :: [Name] -> [Name] -> (Account -> a -> a) -> a -> a
41 go s [] f acc = f (n0:|s) acc
43 go ((Data.List.++) s [n]) ns f (f (n0:|s) acc)
45 -- | Return the concatenation of the given 'Account'.
46 (++) :: Account -> Account -> Account
49 -- | Return an 'Account' from the given list.
50 from_List :: [Name] -> Account
51 from_List = Data.List.NonEmpty.fromList
60 deriving (Data, Eq, Read, Show, Typeable)
62 -- * The 'Filter' type
65 = Pattern_Exact Account
68 deriving (Read, Show, Typeable)