Entries for July 2005
July 21, 2005 at 10:51PM The World’s Simplest (decent) PHP Templating Engine...
...is PHP, so why not use it?
function include_view($view, $vars=NULL) { # Start buffering the generated text. ob_start(); # Process the view. if (!is_null($vars)) { extract($vars, EXTR_OVERWRITE | EXTR_REFS); } include("views/$view.php"); # Grab the generated content and clean up. $content = ob_get_contents(); ob_end_clean(); return $content; }
July 21, 2005 at 11:16PM Memoization in JavaScript
About a fortnight ago, I was poking around the copy of Perl sitting on my laptop. I came across Memoize.pm. Having read the article that inspired it a few years back, I thought I’d take an attempt at implementing something to do the same in JavaScript.
Memoization is a method of increasing the speed of slow referentially transparent functions by caching their arguments and results. This trades a marginal amount of memory space for a potentially huge gain in speed.
Take the canonical definition of the Fibonacci sequence, for instance:
function Fib(n) { if (n < 2) { return n; } return Fib(n - 1) + Fib(n - 2); }
As you can guess, this quickly becomes quite slow once you start using numbers greater than around 20. Once you’re dealing with numbers in the mid-thirties range, it cripples the computer.
The solution is to memoize the function. You can either do it by hand:
var IterMemoFib = function() { var cache = [1, 1]; var fib = function(n) { if (n >= cache.length) { for (var i = cache.length; i <= n; i++) { cache[i] = cache[i - 2] + cache[i - 1]; } } return cache[n]; } return fib; }();
Which is a wee bit of a pain and not exactly readable; or you can get the computer to do it for you:
Fib = Fib.memoize();
Due to technical (browser security) constraints, the arguments for memoized functions can only be arrays or scalar values. No objects.
The code extends the Function object to add the memoization functionality. If the function is a method, then you can pass the object into memoize().
Function.prototype.memoize = function() { var pad = {}; var self = this; var obj = arguments.length > 0 ? arguments[i] : null; var memoizedFn = function() { // Copy the arguments object into an array: allows it to be used as // a cache key. var args = []; for (var i = 0; i < arguments.length; i++) { args[i] = arguments[i]; } // Evaluate the memoized function if it hasn't been evaluated with // these arguments before. if (!(args in pad)) { pad[args] = self.apply(obj, arguments); } return pad[args]; } memoizedFn.unmemoize = function() { return self; } return memoizedFn; } Function.prototype.unmemoize = function() { alert("Attempt to unmemoize an unmemoized function."); return null; }
Elsewhere
- One-Line JavaScript Memoization.
- Timed Memoization - A trivial extension when you want the cached values to be garbage collected after a while. If JavaScript supported weak references, they’d also be useful in allowing the garbage collector to periodically collect memoized results.
July 22, 2005 at 2:50AM Demystifying the GridBagLayout Layout Manager by Peter Haggar
This is an excellent explaination about what must be one of the most puzzling parts of AWT/Swing development: the GridBagLayout layout manager.
The original was an MS Word file. I converted it to LaTeX for my own use, but I think others might find it useful too. I redid the the diagrams to make them clearer, and reworded the text ever-so-slightly because I was, ahem, too lazy to use crossreferences. Enjoy!
Demystifying the GridBagLayout Layout Manager by Peter Haggar (PDF)
If Peter or anybody from IBM would like me to take this down, I’ll do so as soon as possible after I’m contacted.
July 22, 2005 at 1:56PM So, I Was at the Doctor Earlier...
...and she says that I might be anaemic. No surprises there: that’s what I thought myself. However, she was a bit concerned when I brought up the mild blackouts I sometimes get when I stand up too quickly. So, if it’s not anaemia, I don’t know what it is.
She took a fair bit of blood, which I didn’t mind too much. The only thing is that my right arm, and especially my hand, feel a little weak and heavy now for some reason. I expect that’ll fix itself within a couple of hours.
July 22, 2005 at 7:52PM Syndication Fixed
I received an email just a few minutes ago telling me that something was up with the code posted in my blog where it was all coming out on one line. That struck me as odd.
A quick root through it later, and I found that the ColdFusion XMLFormat() function compresses all whitespace into single spaces. That’s not what I’d expected. I hacked past that by using HTMLEditFormat() instead. This is far from an optimal solution.
That lead me to check the linklog feed. It turned out that I wasn’t processing the feed body before I was passing it into the feed. Sorry, guys!
July 23, 2005 at 11:22PM Atom, and Conlangcon
Now that Atom 1.0 is pretty much here, I now feel confident about providing syndication in Atom. I’ll hack it in tomorrow.
And yup, I’ll be attending the Boston Conlangcon seeing as I’ll be in Boston at the time. Not only is this my first time outside the country, but it’ll be my first meeting all you guys, so I’ll probably be a little quiet and untalkative. Don’t worry, I’m just nervous the first time I meet people, and enormity of it all is probably getting to me. Don’t worry though: get a few pints of this into me and I’ll be just fine! ![]()
