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 'Name'.
23 type Account = NonEmpty Name
25 type Map x = TreeMap Name x
27 -- | Return the 'Account' formed by the given 'Name' and 'Name's.
28 account :: Name -> [Name] -> Account
31 -- | Return the number of 'Name's in the given 'Account'.
32 depth :: Account -> Integer
33 depth = Data.Foldable.foldl' (\d -> const $ d + 1) 0
35 -- | Return the given 'Account' without its last 'Name' if any.
36 ascending :: Account -> Maybe Account
37 ascending = NonEmpty.ascending
39 -- | Apply the given function to all the prefixes
40 -- of the given 'Account' (including itself).
41 foldr :: Account -> (Account -> a -> a) -> a -> a
42 foldr (n0:|n0s) = go [] n0s
44 go :: [Name] -> [Name] -> (Account -> a -> a) -> a -> a
45 go s [] f acc = f (n0:|s) acc
47 go ((Data.List.++) s [n]) ns f (f (n0:|s) acc)
49 -- | Return an 'Account' from the given list.
50 from_List :: [Name] -> Account
51 from_List = NonEmpty.fromList
60 deriving (Data, Eq, Show, Typeable)
65 = Pattern_Exact Account
68 deriving (Show, Typeable)