Add distinction between an empty node and a leaf to allow truncating the output right after a node

This commit is contained in:
Tissevert 2019-05-09 16:42:42 +02:00
parent ed5ee22a49
commit 2780b38df2
2 changed files with 4 additions and 2 deletions

View file

@ -16,6 +16,7 @@ instance Functor Stream where
Stream $ (\(a, stream) -> (f a, fmap f stream)) <$> pairs Stream $ (\(a, stream) -> (f a, fmap f stream)) <$> pairs
instance Tree Stream where instance Tree Stream where
getStructure (Stream []) = Leaf
getStructure (Stream pairs) = getStructure (Stream pairs) =
Node $ (\(s, stream) -> (s, getStructure stream)) <$> pairs Node $ (\(s, stream) -> (s, getStructure stream)) <$> pairs

View file

@ -3,7 +3,7 @@ module Tree (
, Structure(..) , Structure(..)
) where ) where
data Structure = Node [(String, Structure)] data Structure = Leaf | Node [(String, Structure)]
class Functor a => Tree a where class Functor a => Tree a where
getStructure :: a String -> Structure getStructure :: a String -> Structure
@ -12,7 +12,8 @@ class Functor a => Tree a where
draw = unlines . getLines . getStructure . fmap show draw = unlines . getLines . getStructure . fmap show
getLines :: Structure -> [String] getLines :: Structure -> [String]
getLines (Node []) = [""] getLines Leaf = [""]
getLines (Node []) = [""]
getLines (Node [(s, structure)]) = showBlock '─' (s, structure) getLines (Node [(s, structure)]) = showBlock '─' (s, structure)
getLines (Node ((s, structure):pairs)) = concat $ getLines (Node ((s, structure):pairs)) = concat $
showBlock '┬' (s, structure) : showBlocks pairs showBlock '┬' (s, structure) : showBlocks pairs