Reimplement showTree, rename it to showStream and expose useful functions from module
This commit is contained in:
parent
06ec115c88
commit
1c93600ecf
1 changed files with 26 additions and 17 deletions
|
@ -1,5 +1,9 @@
|
||||||
module Stream (
|
module Stream (
|
||||||
Stream(..)
|
Stream(..)
|
||||||
|
, fromList
|
||||||
|
, infinite
|
||||||
|
, merge
|
||||||
|
, showStream
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.List (intercalate)
|
import Data.List (intercalate)
|
||||||
|
@ -30,21 +34,26 @@ merge (Stream aPairs) (Stream bPairs) = Stream $ foldl mergeOrAdd aPairs bPairs
|
||||||
(up, []) -> (up, [(b, bStream)])
|
(up, []) -> (up, [(b, bStream)])
|
||||||
(up, (x, xStream):down) -> (up, (x, merge xStream bStream):down)
|
(up, (x, xStream):down) -> (up, (x, merge xStream bStream):down)
|
||||||
|
|
||||||
showTree :: Show a => Stream a -> String
|
showStream :: Show a => Stream a -> String
|
||||||
showTree = intercalate "\n" . getLines
|
showStream = intercalate "\n" . getLines
|
||||||
where
|
|
||||||
getLines (Stream []) = ["●"]
|
|
||||||
getLines (Stream pairs) =
|
|
||||||
concat $ (\(a, stream) -> showElem a $ getLines stream) <$> pairs
|
|
||||||
|
|
||||||
showElem :: Show a => a -> [String] -> [String]
|
getLines :: Show a => Stream a -> [String]
|
||||||
showElem _ [] = []
|
getLines (Stream []) = ["╼"]
|
||||||
showElem a [l] = ["— " ++ show a ++ " —" ++ l]
|
getLines (Stream [(a, stream)]) = showBlock '─' (a, stream)
|
||||||
showElem a (l:ls) =
|
getLines (Stream ((a, stream):pairs)) = concat $
|
||||||
let prefix = "─ " ++ show a ++ " ─┬" in
|
showBlock '┬' (a, stream) : showBlocks pairs
|
||||||
let padding = take (length prefix - 1) $ repeat ' ' in
|
|
||||||
(prefix ++ l) : (pad padding ls)
|
|
||||||
where
|
where
|
||||||
pad _ [] = []
|
showBlocks [] = []
|
||||||
pad padding [x] = [padding ++ '╰' : x]
|
showBlocks [(a, stream)] = [showBlock '╰' (a, stream)]
|
||||||
pad padding (x:xs) = (padding ++ '├' : x) : (pad padding xs)
|
showBlocks ((a, stream):pairs) =
|
||||||
|
showBlock '├' (a, stream) : showBlocks pairs
|
||||||
|
|
||||||
|
showBlock :: Show a => Char -> (a, Stream a) -> [String]
|
||||||
|
showBlock connectingChar (a, stream) =
|
||||||
|
case getLines stream of
|
||||||
|
[] -> []
|
||||||
|
firstLine:otherLines -> (prefix ++ firstLine) : (pad <$> otherLines)
|
||||||
|
where
|
||||||
|
connect = ((if connectingChar `elem` "├┬" then '│' else ' '):)
|
||||||
|
prefix = connectingChar : " " ++ show a ++ " "
|
||||||
|
pad = connect . (take (length prefix - 1) (repeat ' ') ++)
|
||||||
|
|
Loading…
Reference in a new issue