From 2ff9c21b9561395a04136637cefb5fa674765d59 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sat, 11 Oct 2008 04:17:52 +0000 Subject: [PATCH] more account queries --- Ledger/Ledger.hs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/Ledger/Ledger.hs b/Ledger/Ledger.hs index 5b552a911..6fdff112b 100644 --- a/Ledger/Ledger.hs +++ b/Ledger/Ledger.hs @@ -93,6 +93,23 @@ ledgerAccount l a = (accountmap l) ! a ledgerFilteredAccount :: Ledger -> AccountName -> Account ledgerFilteredAccount l a = (filteredaccountmap l) ! a +-- | List a ledger's accounts, in tree order +accounts l = drop 1 $ flatten $ ledgerAccountTree 9999 l + +-- | List a ledger's top-level accounts, in tree order +topAccounts l = map root $ branches $ ledgerAccountTree 9999 l + +-- | Accounts in ledger whose leafname matches the pattern, in tree order +accountsMatching pat l = filter (containsRegex pat . accountLeafName . aname) $ accounts l + +-- | List a ledger account's immediate subaccounts +subAccounts :: Ledger -> Account -> [Account] +subAccounts l a = map (ledgerAccount l) subacctnames + where + allnames = accountnames l + name = aname a + subacctnames = filter (name `isAccountNamePrefixOf`) allnames + -- | List a ledger's transactions. -- -- NB this sets the amount precisions to that of the highest-precision @@ -110,9 +127,17 @@ ledgerAccountTree :: Int -> Ledger -> Tree Account ledgerAccountTree depth l = addDataToAccountNameTree l depthpruned where - nametree = filteredaccountnametree l -- + nametree = accountnametree l depthpruned = treeprune depth nametree +-- that's weird.. why can't this be in Account.hs ? +instance Eq Account where + (==) (Account n1 t1 b1) (Account n2 t2 b2) = n1 == n2 && t1 == t2 && b1 == b2 + +-- | Get a ledger's tree of accounts rooted at the specified account. +ledgerAccountTreeAt :: Ledger -> Account -> Maybe (Tree Account) +ledgerAccountTreeAt l acct = subtreeat acct $ ledgerAccountTree 9999 l + -- | Get a ledger's tree of accounts to the specified depth, filtered by -- the account pattern. ledgerFilteredAccountTree :: Int -> Regex -> Ledger -> Tree Account