[commit: packages/containers] changelog-foldtree, cleaned_bugfix394, master, merge-doc-target, merge-fixes-5.9, merge-restrict-fix-5.8, revert-408-bugfix_394: add a catamorphism on Trees (ce4435b)

git at git.haskell.org git at git.haskell.org
Mon Apr 17 21:40:46 UTC 2017


Repository : ssh://git@git.haskell.org/containers

On branches: changelog-foldtree,cleaned_bugfix394,master,merge-doc-target,merge-fixes-5.9,merge-restrict-fix-5.8,revert-408-bugfix_394
Link       : http://git.haskell.org/packages/containers.git/commitdiff/ce4435be2207942cf1d8419d1a014b1a56a5a4a4

>---------------------------------------------------------------

commit ce4435be2207942cf1d8419d1a014b1a56a5a4a4
Author: Daniel Wagner <me at dmwit.com>
Date:   Tue May 17 10:21:07 2016 -0700

    add a catamorphism on Trees


>---------------------------------------------------------------

ce4435be2207942cf1d8419d1a014b1a56a5a4a4
 Data/Tree.hs | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/Data/Tree.hs b/Data/Tree.hs
index 4cd05a5..c35d0ed 100644
--- a/Data/Tree.hs
+++ b/Data/Tree.hs
@@ -30,7 +30,7 @@ module Data.Tree(
     -- * Two-dimensional drawing
     drawTree, drawForest,
     -- * Extraction
-    flatten, levels,
+    flatten, levels, foldTree,
     -- * Building trees
     unfoldTree, unfoldForest,
     unfoldTreeM, unfoldForestM,
@@ -156,6 +156,11 @@ levels t =
         takeWhile (not . null) $
         iterate (concatMap subForest) [t]
 
+-- | Catamorphism on trees.
+foldTree :: (a -> [b] -> b) -> Tree a -> b
+foldTree f = go where
+    go (Node x ts) = f x (map go ts)
+
 -- | Build a tree from a seed value
 unfoldTree :: (b -> (a, [b])) -> b -> Tree a
 unfoldTree f b = let (a, bs) = f b in Node a (unfoldForest f bs)



More information about the ghc-commits mailing list