Visualize a tree: Difference between revisions
m
→JS Vertically centered tree: Added a comment
m (→JS Vertically centered tree: Added a comment) |
|||
Line 1,319:
// https://doisinkidney.com/snippets/drawing-trees.html
const
lmrFromStrings = xs => {
const [ls, rs] = Array.from(splitAt(
Line 1,339 ⟶ 1,340:
lng = xs.length,
[nChars, x] = Array.from(wsTree.root);
// LEAF NODE --------------------------------------
return 0 === lng ? (
Tuple3([], '─'.repeat(w - nChars) + x, [])
// NODE WITH SINGLE CHILD -------------------------
) : 1 === lng ? (() => {
const indented = leftPad(1 + w);
Line 1,348 ⟶ 1,353:
indented
)(f(xs[0]));
// NODE WITH CHILDREN -----------------------------
})() : (() => {
const
Line 1,383 ⟶ 1,390:
const
measuredTree = fmapTree(
const s =
return
},▼
x => ' ' + x + ' '▼
▲ ), tree
),
levelWidths = init(levels(measuredTree))
Line 1,395 ⟶ 1,399:
(a, level) => a.concat(maximum(level.map(fst))),
[]
levelWidths.reduceRight(▼
)(measuredTree)▼
);
▲ const treeLines = stringsFromLMR(
▲ levelWidths.reduceRight(
▲ )(measuredTree)
return unlines(
blnPruned ? (
Line 1,489 ⟶ 1,492:
length: 3
});
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
Line 1,505:
return unit.concat.apply(unit, xs);
})() : [];
// fmapTree :: (a -> b) -> Tree a -> Tree b
|