Visualize a tree: Difference between revisions

m
Line 1,319:
// https://doisinkidney.com/snippets/drawing-trees.html
const
// Lefts, )Middle, treeRights
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(
compose(v => {
const s => {' ' + v + ' ';
return const lng = Tuple(s.length;, s)
return Tuple(lng}, s)tree
},
x => ' ' + x + ' '
), tree
),
levelWidths = init(levels(measuredTree))
Line 1,395 ⟶ 1,399:
(a, level) => a.concat(maximum(level.map(fst))),
[]
}),
const treeLines = stringsFromLMR(
levelWidths.reduceRight(
lmrBuild, x => ' ' + x + ' '
)(measuredTree)
);
 
const treeLines = stringsFromLMR(
levelWidths.reduceRight(
lmrBuild, x => x
)(measuredTree)
);
return unlines(
blnPruned ? (
Line 1,489 ⟶ 1,492:
length: 3
});
 
// any :: (a -> Bool) -> [a] -> Bool
const any = (p, xs) => xs.some(p);
 
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
Line 1,505:
return unit.concat.apply(unit, xs);
})() : [];
 
// Flexibly handles two or more arguments, applying
// the function directly if the argument array
// is long enough for complete saturation,
// or recursing with a concatenation of any existing and
// newly supplied arguments, if gaps remain.
 
// curry :: ((a, b) -> c) -> a -> b -> c
const curry = (f, ...args) => {
const
n = f.length,
go = xs => n <= xs.length ? (
f(...xs)
) : function() {
return go(xs.concat(Array.from(arguments)));
};
return go(args);
};
 
// fmapTree :: (a -> b) -> Tree a -> Tree b
9,659

edits