Visualize a tree: Difference between revisions

m
Line 1,315:
 
// drawTree2 :: Bool -> Bool -> Tree String -> String
const drawTree2 = blnCompressedblnCompact => blnPruned => tree => {
// Tree design and algorithm inspired by the Haskell snippet at:
// https://doisinkidney.com/snippets/drawing-trees.html
Line 1,336:
const
leftPad = n => s => ' '.repeat(n) + s,
conS = x => xs => x + xs,
xs = wsTree.nest,
lng = xs.length,
Line 1,351 ⟶ 1,350:
})() : (() => {
const
conS cFix = x => xs => x + xs,
treeFix = (l, m, r) => compose(
stringsFromLMR,
fghOverLMR(conScFix(l), conScFix(m), conScFix(r))
),
_x = '─'.repeat(w - nChars) + x,
Line 1,369:
)(lmrFromStrings(
intercalate(
blnCompressedblnCompact ? [] : ['│'],
[treeFix(' ', '┌', '│')(lmrs[0])]
.concat(init(lmrs.slice(1)).map(
Line 1,384:
measuredTree = fmapTree(
compose(
bindFn(length,s Tuple),=> {
lmrBuild, x const lng => xs.length;
return Tuple(lng, s)
)) },
x => ' ' + x + ' '
), tree
Line 1,393 ⟶ 1,396:
[]
);
 
const treeLines = stringsFromLMR(
levelWidths.reduceRight(
)(measuredTree)lmrBuild, x => x
)(measuredTree)
);
return unlines(
(blnPruned ? (
curry(treeLines.filter)(s => any(
cs => !s.split(''.includes(c),
chars.some(s)c => !' │'.includes(c))
) : identity )(stringsFromLMR(
) : levelWidths.reduceRight(treeLines
lmrBuild, x => x
)(measuredTree)
))
);
};
Line 1,444 ⟶ 1,450:
// strTrees :: String
const strTrees = ([
'Compacted (parents not all vertically centered):',
drawTree2(true)(false)(tree2),
'Fully expanded, with vertical centering:',
Line 1,486 ⟶ 1,492:
// any :: (a -> Bool) -> [a] -> Bool
const any = (p, xs) => xs.some(p);
 
// bindFn (>>=) :: (a -> b) -> (b -> a -> c) -> a -> c
const bindFn = (f, bop) =>
// Binary operator applied over f x and x.
x => bop(f(x), x);
 
// chars :: String -> [Char]
const chars = s => s.split('');
 
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
Line 1,525 ⟶ 1,523:
return go(args);
};
 
// filter :: (a -> Bool) -> [a] -> [a]
const filter = (f, xs) => xs.filter(f);
 
// fmapTree :: (a -> b) -> Tree a -> Tree b
Line 1,613 ⟶ 1,608:
 
// splitAt :: Int -> [a] -> ([a], [a])
const splitAt = (n, xs) => Tuple(xs.slice(0, n), xs.slice(n));
Tuple(xs.slice(0, n), xs.slice(n));
 
// unlines :: [String] -> String
Line 1,622 ⟶ 1,618:
})();</lang>
{{Out}}
<pre>Compacted (parents not all vertically centered):
 
┌ 4
Line 1,653 ⟶ 1,649:
│ ┌─── Iota
└ Delta ┼── Kappa
└─ Lambda </pre>
 
====Decorated outline====
9,659

edits