1 {-# LANGUAGE DeriveDataTypeable #-}
2 module Hcompta.Account where
4 import Data.Data (Data)
5 import qualified Data.Foldable
6 import qualified Data.List
7 import qualified Data.List.NonEmpty as NonEmpty
8 import Data.List.NonEmpty (NonEmpty(..))
9 import Data.Typeable (Typeable)
10 import Data.Text (Text)
12 import qualified Hcompta.Lib.NonEmpty as NonEmpty
13 import Hcompta.Lib.Regex (Regex)
14 import Hcompta.Lib.TreeMap (TreeMap)
16 -- * The 'Account' type
18 -- | An 'Account' is a non-empty list of 'Name'.
19 type Account = NonEmpty Name
21 type Map x = TreeMap Name x
23 -- | Return the 'Account' formed by the given 'Name' and 'Name's.
24 account :: Name -> [Name] -> Account
27 -- | Return the number of 'Name's in the given 'Account'.
28 depth :: Account -> Integer
29 depth = Data.Foldable.foldl' (\d -> const $ d + 1) 0
31 -- | Return the given 'Account' without its last 'Name' if any.
32 ascending :: Account -> Maybe Account
33 ascending = NonEmpty.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 an 'Account' from the given list.
46 from_List :: [Name] -> Account
47 from_List = NonEmpty.fromList
56 deriving (Data, Eq, Read, Show, Typeable)
61 = Pattern_Exact Account
64 deriving (Read, Show, Typeable)