Visualize a tree: Difference between revisions
m
→JS Vertically centered tree: Minor tidying
m (→JS Vertically centered tree: Minor tidying) |
|||
Line 1,315:
// drawTree2 :: Bool -> Bool -> Tree String -> String
const drawTree2 =
// 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
treeFix = (l, m, r) => compose(
stringsFromLMR,
fghOverLMR(
),
_x = '─'.repeat(w - nChars) + x,
Line 1,369:
)(lmrFromStrings(
intercalate(
[treeFix(' ', '┌', '│')(lmrs[0])]
.concat(init(lmrs.slice(1)).map(
Line 1,384:
measuredTree = fmapTree(
compose(
return Tuple(lng, s)
x => ' ' + x + ' '
), tree
Line 1,393 ⟶ 1,396:
[]
);
const treeLines = stringsFromLMR(
levelWidths.reduceRight(
)(measuredTree)
);
return unlines(
) :
▲ 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);
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
Line 1,525 ⟶ 1,523:
return go(args);
};
// 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));
// 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
====Decorated outline====
|