Sort an outline at every level: Difference between revisions

Content added Content deleted
m (→‎{{header|Python}}: (Repasted output without (tab->space) clipboard conversion))
m (→‎{{header|Haskell}}: Slightly simplified [Tree String] -> String serialization)
Line 452: Line 452:
subForest $
subForest $
foldTree (\x xs -> Node x (L.sortBy cmp xs)) (Node "" forest)
foldTree (\x xs -> Node x (L.sortBy cmp xs)) (Node "" forest)
in Right $ outlineFromForest (<>) indentUnit sortedForest
in Right $ outlineFromForest indentUnit sortedForest



--------------------------- TESTS --------------------------
--------------------------- TESTS --------------------------

main :: IO ()
main :: IO ()
main =
main =
Line 546: Line 548:
indentUnit = maybe 1 fst (L.find ((0 <) . fst) pairs)
indentUnit = maybe 1 fst (L.find ((0 <) . fst) pairs)


outlineFromForest :: (T.Text -> a -> T.Text) -> T.Text -> [Tree a] -> T.Text
outlineFromForest :: T.Text -> [Tree T.Text] -> T.Text
outlineFromForest showRoot tabString forest = T.unlines $ forest >>= go ""
outlineFromForest tabString forest = T.unlines $ forest >>= go ""
where
where
go indent node =
go indent node =
showRoot indent (rootLabel node) :
indent <> rootLabel node :
(subForest node >>= go (T.append tabString indent))
(subForest node >>= go (T.append tabString indent))



------ OUTLINE CHECKING - INDENT CHARACTERS AND WIDTHS -----
------ OUTLINE CHECKING - INDENT CHARACTERS AND WIDTHS -----

consistentIndentUnit :: [T.Text] -> Either T.Text T.Text
consistentIndentUnit :: [T.Text] -> Either T.Text T.Text
consistentIndentUnit prefixes = minimumIndent prefixes >>= checked prefixes
consistentIndentUnit prefixes = minimumIndent prefixes >>= checked prefixes