«function vars/args as dictionary» by vividsnow

on 23 Nov'15 05:04 in

shortcuts to get function variables/arguments state as dictionary

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// variables
({ var aa = 3, bb = aa + 1, cc = 5;
{ |d v|
	d.inject((), { |a i k| a[i] = v[k] })
}.(thisFunctionDef.varNames, this.getBackTrace.vars);
}.())

// arguments
({ |aa=1 bb=3|
{ |d v|
	d.inject((), { |a i k| a[i] = v[k] })
}.(thisFunctionDef.argNames, this.getBackTrace.args);
}.())


//both
({ |c=3 r=#[3,1]| var aa = 3, bb = r;
{ |trace|
	['arg','var'].inject((), { |a type|
		var trace_vals = trace.perform((type++'s').asSymbol);
		a[type] = trace.functionDef.perform((type++'Names').asSymbol).inject(
			(), { |a i k| a[i] = trace_vals[k]}) });
}.(this.getBackTrace);
}.(r:[4,1]));
raw 617 chars (focus & ctrl+a+c to copy)
reception
comments