summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@shackspace.de>2014-12-25 03:46:47 +0100
committertv <tv@shackspace.de>2014-12-25 03:46:47 +0100
commit2d3088c15975ed129fa12e779769396b1ede2883 (patch)
treeb3118ac98c7753adeee921102bbf72068be0410c
parent2054ab9a2d9fe4b3ea3890078a16d85a4d02aa4e (diff)
import TreeSearch from lass
-rw-r--r--TreeSearch.hs19
1 files changed, 19 insertions, 0 deletions
diff --git a/TreeSearch.hs b/TreeSearch.hs
new file mode 100644
index 0000000..938b076
--- /dev/null
+++ b/TreeSearch.hs
@@ -0,0 +1,19 @@
+module TreeSearch where
+
+import Data.Tree.Zipper
+import Data.Maybe
+
+-- findTree :: PosType t => (a -> Bool) -> TreePos t a -> Maybe (TreePos t a)
+findTree :: (a -> Bool) -> TreePos Full a -> Maybe (TreePos Full a)
+findTree p loc = if p (label loc)
+ then Just loc
+ else depthFirst loc >>= findTree p
+
+depthFirst :: TreePos Full a -> Maybe (TreePos Full a)
+depthFirst loc = case firstChild loc of
+ Just x -> Just x
+ Nothing -> case next loc of
+ Just x -> Just x
+ Nothing -> case parent loc of
+ Just x -> next x
+ Nothing -> Nothing