Skip to content

Instantly share code, notes, and snippets.

@catfact
Created June 12, 2024 19:28
Show Gist options
  • Save catfact/df228534eefe27d60a18581050e8ba0c to your computer and use it in GitHub Desktop.
Save catfact/df228534eefe27d60a18581050e8ba0c to your computer and use it in GitHub Desktop.
//--------------
// describe one thing
/*
first difficulty: classes
SC has a flexible and heterogenous class system, with innumerable classes inherited from `Object`, compared to the limited number of data types in MATLAB. there is no concept of primitive or literal types.
so, our "describe" function needs to explicitly handle types of objects for which numerical description makes sense.
implementation below is probably neither exhaustive (e.g. doesn't handle `Char`) nor exclusive enough (collections may contain indescribable things) but it is a start
*/
~describe = { arg displayName, x;
var class = x.class;
var value, min, max, mean;
var y = nil;
postln("-- describing: ");
postln(x);
if (class.superclasses.includes(Collection), {
///-------------------
/// bit of a hack - filter out non-comparable items
x = x.select({arg item; item.class.superclasses.includes(Magnitude)});
///----------------
postln("this is a collection: ");
postln(x);
y = (displayName: displayName, classname: class.name, displayValue: class.name, min: x.minItem, max: x.maxItem, mean: x.mean);
});
if (class.superclasses.includes(Number), {
postln("this is a number");
y = (displayName: displayName, classname: class.name, displayValue: x);
});
if (y.isNil, {
postln("not describable");
}, {
postln([y.name, y.displayValue]);
});
postln("");
y
};
~f = 555.555;
~i = 777;
~a = Set[1, 2, 3, 4, 5];
~b = Array.fill(100, {1.0.rand});
~describe.value("f", ~f);
~describe.value("i", ~i);
~describe.value("a", ~a);
~describe.value("b", ~b);
//--------------
// describe everything
/*
second difficulty: scopes and conventions
the single-letter interpreter variables (`a`, `b`, `c` etc.) are stored in individual fields of the `Interpreter` class. there are strong conventions, like `e` being the current global environment and `s` being the default Server. but these are not guarantees and the fields can be overwritten.
for demonstration purposes, let's just look at global variables defined in the current environment - those prefixed with `~`.
*/
~descriptions = currentEnvironment.keys.asArray.sort.collect({ arg key;
var value = currentEnvironment[key];
postln("about to describe: ");
[key, value].postln;
postln("");
~describe.value(key, value)
}).select({|x| x.notNil});
//---------------
// display and interact
{
var win;
var n = ~descriptions.size;
var w = 80;
var h = 30;
var g = 1;
var b, c;
var linew = 7 * (w+g);
win = Window.new("environment variables", Rect(0, 0, linew, 300));
win.view.decorator = FlowLayout(win.view.bounds);
StaticText(win, Rect(0, 0, w, h)).string_("name");
StaticText(win, Rect(0, 0, w, h)).string_("class");
StaticText(win, Rect(0, 0, w, h)).string_("value");
StaticText(win, Rect(0, 0, w, h)).string_("min");
StaticText(win, Rect(0, 0, w, h)).string_("max");
StaticText(win, Rect(0, 0, w, h)).string_("mean");
win.view.decorator.nextLine;
b = ScrollView(win, Rect(0, 0, win.view.bounds.width, win.view.bounds.height-h));
c = CompositeView(b, Rect(0, 0, linew, n * (h+g) + 4));
c.decorator = FlowLayout(c.bounds).gap_(Point(g, g));
n.do({ arg i;
var d = ~descriptions[i];
var valStr, minStr, maxStr, meanStr;
postln("-- adding a line for variable: ");
[ d.displayName, d.displayValue].postln;
postln("");
valStr = d.displayValue.asString.copyRange(0, 8);
minStr = d.min.asString.copyRange(0, 8);
maxStr = d.max.asString.copyRange(0, 8);
meanStr = d.mean.asString.copyRange(0, 8);
Button(c, Rect(0, 0, w, h)).states_([[d.displayName, Color.black, Color.white]])
.action_({ currentEnvironment[d.name].inspect });
StaticText(c, Rect(0, 0, w, h)).string_(d.classname).background_(Color.white);
StaticText(c, Rect(0, 0, w, h)).string_(valStr).background_(Color.white);
StaticText(c, Rect(0, 0, w, h)).string_(minStr).background_(Color.white);
StaticText(c, Rect(0, 0, w, h)).string_(maxStr).background_(Color.white);
StaticText(c, Rect(0, 0, w, h)).string_(meanStr).background_(Color.white);
c.decorator.nextLine;
});
win.front;
}.defer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment