«convert arrays to graphviz dot format» by julian.rohrhuber

on 09 Jun'15 12:31 in arraysdotnested

Displays a tree from nested arrays for graphviz

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(
var labels = "", links = "", i = 0;

var doDict = { |x, func|
	if(x.isKindOf(Dictionary)) {
		x.keysValuesDo { |key, val| func.value(val, i, key) }
	} {
		x.do(func)
	}
};

var addNode = { |i, j, key, label = ""|
	labels = labels ++ "\n% [label=\"%\"]".format(i, label);
	links = links ++ "\n% -> %".format(j, i);
	if(key.notNil) { links = links ++ " [label=\"%\"; fontname=Courier]".format(key) };
};

f = { |coll|
	var j = i, str;
	doDict.(coll, { |x, k, key|
		i = i + 1;
		if(x.isCollection) {
			addNode.(i, j, key, "");
			f.(x);
		} {
			addNode.(i, j, key, x.cs);
		}
	});
	str = "\n\ndigraph {\n" ++ labels ++ "\n" ++ links ++ "\n}";
};

)

f.([1, 2, 3, [2, 3, [1, 2, 3]]];);
f.([1, 2, 3, [2, (a: 3, b: [9, 2], c: { 2 + 3 }), [1, 2, 3]]];);
raw 785 chars (focus & ctrl+a+c to copy)
reception
comments