Skip to content

Instantly share code, notes, and snippets.

@doivosevic
Created December 20, 2014 19:30
Show Gist options
  • Save doivosevic/603d1f47ccdadba7eb94 to your computer and use it in GitHub Desktop.
Save doivosevic/603d1f47ccdadba7eb94 to your computer and use it in GitHub Desktop.
showSTENs :: Int -> [StackTrace] -> String
showSTENs tabLen = unlines . showSTENs' 0 . build
where
showTabs :: Int -> String
showTabs numSpaces = replicate numSpaces ' '
showSTENs' :: Int -> [STENode] -> [String]
showSTENs' tabs [] = [""]
showSTENs' tabs stens = concat $ map showSTEN (groupAndJoin . sort $ stens)
where
showSTEN (STENode ste []) = [showTabs (tabs * tabLen) ++ show ste]
showSTEN (STENode ste stens') = (showTabs (tabs * tabLen) ++ show ste) : (showSTENs' (tabs+1) stens')
groupAndJoin :: [STENode] -> [STENode]
groupAndJoin stens = map merge (group stens)
where
merge :: [STENode] -> STENode
merge stens@((STENode ste _):rest) = STENode ste (concat . map (\(STENode _ ss) -> ss) $ stens)
build :: [StackTrace] -> [STENode]
build traces = map buildList traces
where
buildList :: StackTrace -> STENode
buildList trace = foldr makeNewSTENode (STENode (last trace) []) (init trace)
makeNewSTENode :: StackTraceElement -> STENode -> STENode
makeNewSTENode parent child = STENode parent [child]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment