How I became friends with Function Objects [Rock bands and Programming]

I’ve occasionally identified with some self-defining Rock Star archetype many years now. I can’t put my finger on exactly what, and it’s certainly not for some sex, drugs, and sensationalism power trip, but some aloof-yet-serious image served as a touchstone for some of my personal development. Beyond that, I love music and enjoy singing. I’m on the road to mastering the keys and throatstrings, and I’ll probably learn other musical expressions in the future.

I visited Kansas almost a year ago, being the transport of a friend in the process. His band put on a show just before I left and practiced for it the whole week I was there. Getting to see these guys, far more accomplished musicians than I, practice and put on a show was one of the driving goals for myself going on the trip.

I’m a fan of figure-it-out-yourself approaches and I’d call it a personal failure to ask someone to show me how to make music. These people are on a different level than myself, so watching them and participating in the events ensuing was a treat to the researcher’s hunger that didn’t completely spoil my private pioneering. This same will-of-the-novice carried it’s seemingly inherent stumble. It’d lead through a self-imposed embarrassment by zealousness and mirror sickness, being spiritually trapped in an image.

I knew this would happen on some level, but I also knew that what I would gain in experience would be worth any pain of the experience, which I should include with the three weeks of 10-hour work days I paid for the time off work, and even the effort to make THAT possible.

Music was then, and is now a serious endeavor of my life — something I put great care into bending my life to benefit. These events sank in with time, and as all things, it didn’t just go away.


I would later trace back an idea’s origins to this trip. I have images in my head still of messing with pedals and other sound manipulators that use a standard analog audio cable ports (as far as I understand them) to manipulate data (sound) as it streams through.


At the time I recalled this, I was biding the annoyance of rewriting code.
“We need to wait for these assets to load, asynchronously, then begin a single action.”
“I need loading data from many different systems to funnel into one feedback, the all-encompassing load bar.”
Hidden synapse firing. I remembered the function object, with which I’d recently been replacing much older API use of the “Function” class in Actionscript3. As Jackson Dunstan pointed out in his article on the subject, AS3’s “function pointers” are slower than the easily crafted “Runnables”, Function Objects.

Simply put:

class Runnable
 public virtual void run( ) = 0;
interface Runnable
 function run(): void //I prefer the (args:Array = null) parameter

My C++ is a bit rusty. I could come up with a better solution. I’d probably overload the parenthesis operator. I saw stdargs.h for the first time recently. That could be used to replicate my args:Array=null functionality from AS3, but it looks annoying to use.

So the ideas came together and there’s no other way it would’ve happened. It might not be the most novel thing to the world, but I was quite proud of what I was able to create in the way of tools.
By allowing classes/systems to create specialized Function Objects (with references to their roots) that all derived from the same interface, I could allow for, and would later develop, a large number of tools for data-flow and complexity management. Most of these were little junctions you’d create and use in local scope, who would later delete themselves (or have themselves deleted, depending on the language/implEmentation) when their task is complete.

And excellent example and starting point is what I called the Gate class:

Give it a final Runnable or list of them (sort them with priority values to make events!) to fire upon completion. Tell it to create Runnables via public function. When all the passed out Runnables are called, fires the final Runnable/list it’s stored. Can be locked while being set up. Can delete self on completion (Careful!)

This can be tailored to fit a scenario (child class, maybe) like keyed Runnables. Each created Runnable “trigger” has a key, duplicates just open the same lock. Works like an or (||) switch. You could also have it reset after completion for cycling logic, or have them pass data and do math to get numeric filters and funnels. That load bar got a lot easier!

Since they’re faster than Function (pointer) calls in AS3, Runnables make more sense for callbacks, there. I’m quite certain this will never be the case with lower level languages like C++, but if made fast enough, this could offer flexibility well worth the overhead. The ability to store data, in particular, has it’s own benefits to be discovered. For instance, you can store another Runnable as a “Callthrough” callback. Use this new instantiation where you had a callback Runnable going somewhere like a conditional interrupt, data gathering, or filter, like putting a pedal between your keyboard and amp.

It’s a less strong-arm hierarchical method of data-flow. It can also do complexity-flow, something I’ve not heard much about, even in concept. It takes some intuition to use well,  I think, and I wouldn’t use it densely in group projects without strong documentation and, of course, agreement and comprehension. Flexible code is always worth the effort in my eyes. It tends to be more reusable and generic, ie Elemental.

Sorry I don’t have code to offer with the above example. I didn’t think it was too hard to grasp, and you can write your own :P


About Elisha

Craftsman. Helminmancer. Smoulder-Banshee. The Fiery Flender.
This entry was posted in Game Craft, Music, Personal. Bookmark the permalink.

2 Responses to How I became friends with Function Objects [Rock bands and Programming]

  1. Nice article!

    If you want your runnables to take arbitrary arguments, consider instead using var args:

    function run(…args): void;

    This avoids having to do costly Array creation (and possibly garbage collection) and Array writes just to pass some arguments. Instead, you can pass them more naturally and—as of Flash Player 10.2—much more efficiently like so:“String”, 42, true, null, undefined, 61.25);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s