/ http://xahlee.org/PerlMathematica_dir/Matica.html / examples are indented / there's a better (or at least different) way to represent trees: / 0 1 2 3 4 5 6 7 8 9 0 1 3 4 5 v:0 0 0 0 1 1 2 3 3 3 3 3 5 5 5 / structure, no data nodecount:#: nodes:!nodecount@ leaves:{n@&~(n:nodes x)_lin x} nonleaves:{nodes[x]_di leaves x} nodes v leaves v nonleaves v leafcount:#leaves@ nonleafcount:#nonleaves@ nodecount v leafcount v nonleafcount v nodepaths:{x\'nodes x} leafpaths:{x\'leaves x} nonleafpaths:{x\'nonleaves x} nodepaths v leafpaths v nonleafpaths v data:{[v](nodes[v]_lin leaves v)*nodecount[v]_draw 0} :d:data v aggr:{@[z;nodepaths y;x;z]} :s:aggr[+;v;d] \ / range range1:!: range2:{x+!1+y-x} range3:{x+z*!1+_(y-x)%z} range1 5 range2[5;10] range3[5;7;.3] range3[5;-4;-2] / depth depth:{:[(@x)|~#x;0;1+|/_f'x]} depth() depth 1 2 3 depth(1;(2;(3;,4));1) / leaves, nodes, paths .. t:((1;(2;3 4 5));(,5;6);6 7 8 9 10) t:(1;(2;3 4);,5;6) leaves:,// leaves() leaves t leafcount:#leaves@ leafcount() leafcount t leafpaths:{:[@x;,();,/(!#x),/:'_f'x]} leafpaths() leafpaths t subpaths:{(1+!#x)#\:x} nonleafpaths:?,/(subpaths -1_)'leafpaths@ nonleafpaths() nonleafpaths t nonleafcount:#nonleafpaths@ nodecount:+/(leafcount;nonleafcount)@\: leafcount t nonleafcount t nodecount t / dimensions is just shape dimensions:^: dimensions() dimensions(5;98;`x) dimensions(`a`b`c;1 2 3) dimensions@2 3 1#0 / part is just index-at-depth part:. part[2 3 4#!26;0 1 2] / level (doesn't seem useful) / transpose is just flip transpose:+: dimensions transpose 2 3 4#!26 / apply at depth at:{y{x'}/x} / flattenat is just ,/ at depth flattenat:{at[,/;y]x} dimensions flattenat[2 3 4 5#!120;2] / rotateleft is just 1! rotateleft:1! / function is just lambda definition {[i;j]i+j}[3;4] / map map2:@ map3:{:[z~-1;map2[x]y;y{y@x}/x at'range2 . 2#z]} map2[2+;t] map3[|:;t;1 2]