projects
/
haskell-directory.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
[project @ 2005-11-29 14:31:59 by ross]
[haskell-directory.git]
/
Data
/
Tree.hs
diff --git
a/Data/Tree.hs
b/Data/Tree.hs
index
5a30470
..
e0a7cb6
100644
(file)
--- a/
Data/Tree.hs
+++ b/
Data/Tree.hs
@@
-28,9
+28,13
@@
module Data.Tree(
import Prelude
#endif
import Prelude
#endif
+import Control.Applicative (Applicative(..))
import Control.Monad
import Control.Monad
-import Data.Sequence (Seq, empty, singleton, (<|), (|>), fromList, toList,
+import Data.Monoid (Monoid(..))
+import Data.Sequence (Seq, empty, singleton, (<|), (|>), fromList,
ViewL(..), ViewR(..), viewl, viewr)
ViewL(..), ViewR(..), viewl, viewr)
+import Data.Foldable (Foldable(foldMap), toList)
+import Data.Traversable (Traversable(traverse))
import Data.Typeable
#include "Typeable.h"
import Data.Typeable
#include "Typeable.h"
@@
-57,6
+61,12
@@
instance Functor Tree where
mapTree :: (a -> b) -> (Tree a -> Tree b)
mapTree f (Node x ts) = Node (f x) (map (mapTree f) ts)
mapTree :: (a -> b) -> (Tree a -> Tree b)
mapTree f (Node x ts) = Node (f x) (map (mapTree f) ts)
+instance Traversable Tree where
+ traverse f (Node x ts) = Node <$> f x <*> traverse (traverse f) ts
+
+instance Foldable Tree where
+ foldMap f (Node x ts) = f x `mappend` foldMap (foldMap f) ts
+
-- | Neat 2-dimensional drawing of a tree.
drawTree :: Tree String -> String
drawTree = unlines . draw
-- | Neat 2-dimensional drawing of a tree.
drawTree :: Tree String -> String
drawTree = unlines . draw