Skip to content

Instantly share code, notes, and snippets.

@anthonyquizon
Last active April 12, 2025 09:39
Show Gist options
  • Save anthonyquizon/fd7201ff3e90f30933818cf5770bb291 to your computer and use it in GitHub Desktop.
Save anthonyquizon/fd7201ff3e90f30933818cf5770bb291 to your computer and use it in GitHub Desktop.
#
# Example of how to render an ascii tree from a parent vector
#
Draw⇐{
d←(⊢∾1+⊏)´p𝕩 # p: parent vector, d: depth vector
w←{(1-˜)(𝕨(⊣⊔˜)p)(⊢∾¨˜)((𝕩𝕨p)/)𝕩}´d⊔↕≠p # order depths depending on parent groups
n←(1+wp)˘(1+⌈´d)w=wd # matrix of ordered depth path cut off
n↩(0≠∨˝n)/˘n>(↕≠)({«𝕨 𝕩}˘)n # shift columns under parent node and remove empty space
s0<∊˘ne0<()˘nhs+`-˘em0<hnvse # s: start, e: ends, h: horizontals, m: middles, v: verticals
b" ─┬├┐│"˜(0˜≠⍉n)1↓⌈´hmsevר1+↕5 # branches - nullify root layer. 1: horizontal, 2: middle, 3: start, 4: end, 5: vertical,
o˘(0)" ""·"¨n # convert nodes to characters
•Out˘b{𝕨(@+10)𝕩}˘o # combine branches and nodes and print
}
Draw 001
Draw 0000
Draw 0010
Draw 00110
Draw 00101
Draw 000222
Draw 0002220
Draw 00022200
Draw 00120204224
Draw 001201041
Draw 0012333
Draw 00123444
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment