Small Hacks of Dubious Utility
These are some minor programs, not terribly long, but typically meant to push the limits of my skill (or the implementation language) in some manner. Starting with cold-hashes, they're even distributed with tests. All are currently available under the Apache 2.0 license.
- Objects: Hashtable and Closures (in Perl) and the complementary Closure Object (in PHP 5)
- The Perl code demonstrates that it is possible to build an object system out of hash-tables (for public
methods/properties) and closure (for protected and private methods/properties). This supports all three access levels,
while using none of Perl's built-in object mechanisms. It uses only common functional programming features.
The PHP code implements the opposite concept: a closure in a nice OO format. Apparently, taking this concept farther can result in functors, but I didn't know that when writing the code. (Also, PHP was chosen because I needed a language that didn't already have closure, as that would be a tad pointless.) - Cold hashes (in Ruby 1.8)
- Append-only hashes. When a new key is set, its value becomes cold, and can't be modified. This is in contrast with the
Object#freezemethod provided by Ruby, which disallows any change to the object at all. The idea behind this is to allow for hashes to be used like a data object, with the ability to notify the programmer if there is a name clash in setting the keys. - Stackable class (in PHP 5)
- The goal here is to make code like
$log = new LogStacker("where-am-I")merely push the string describing where the flow of control is (the context) onto the stack of logs. When the scope exits and __destruct() is called, then the relevant string should be popped from the stack. In the meantime, calling$log->write("Oh no!")should write the full set of context to the log, to capture the essence of how execution reached the function doing the actual logging.
The Stackable class provides most of the support code for doing exactly that, and also takes care so that creating two objects in the same scope and explicitly destroying the first one doesn't cause the whole system to break down. - Lisp Conditions (in Ruby 1.8 or in PHP 5)
- For details on Lisp's conditions, signals, and restarts, see Chapter 19 of Peter Seibel's excellent book, Practical Common Lisp. For my implementations, I've tried to capture the essence of the system, although the details may diverge from Lisp itself. The PHP version depends on the stackable class, above, in order to stack the signal handlers.
- Overload classes (in PHP 5)
- I don't have a particularly good name for this. Combining a mild hack with eval() and a naming convention, overloadable classes use the __autoload() mechanism to allow for extending classes (one time only) without changing their name. This was originally conceived for testing new statically-called interfaces, for which callers must know the class name at compile time. It could also be used to add helpful internal-state-inspection to unit tests. (But down that road lies madness....)