code

Optimizing distance calculations

My brother wrote about calculating distances with PostGIS. I followed up with some suggestions, but WordPress seems to be eating my comments (strip_tags() is mutilating them, stripping anything after a “<”). So I’m following up here. Most of my first comment got eaten; I went on to optimize the query significantly. It should have a WHERE s.zip = 10001, which makes it O(n+1). Obviously, there’s no point in an unconstrained O(nn) JOIN, since we only care about the distance of one zip to the others. What I described was an optimization where you use the JOIN to narrow the resultset to an approximate area, then refine it with distance_sphere(). So you tighten the focus of the expensive operation to the results which are most likely to be relevant. In other words, if you’re looking for something in Delaware, don’t bother with Nevada.

Another undocumented iPhone 2.2 feature

More improvements to app updates. When you update an app, the incomplete app icon immediately replaces the old app icon. It used to be that the old app would continue to exist, while a new icon was added for the updated version. When the update was finished, it replaced the old app. This happens earlier, so there’s less jumping around.

More on the podcast audio playback

As I mentioned earlier, I really liked the ability of 1.x iPhones to play back only the audio portion of a video podcast. Video podcasts appeared under both Podcasts and Videos. If playback was initiated from the former, only the audio would play, while the audio and video played when initiated from the latter. This was very convenient when I needed to watch part of a podcast, lock and pocket the phone, continue listening, then resume watching later. While I’m a bit fuzzy on the specific version, this feature was removed around v2.0, replaced with the ability to play videos back when the handset was in a vertical orientation. While I prefer the old behavior, I see no reason why you shouldn’t be able to have it both ways. If the audio simply continued playing when you locked the phone during Podcast video playback, that would be fine. I’ve found two workarounds, neither of which I find satisfying.
  1. Don’t lock the phone, but stick it in my pocket.
  2. Download the audio-only version of the podcast as well as the video.
The first suffers from two severe deficiencies.

Brief iPhone 2.2 notes

I jailbroke my iPhone last week to see what was happening in the homebrew scene. Not all that much, really, so I upgraded to the official 2.2 this morning. Both times I’ve jailbroken (2.1 and 1.1.3), my iPhone has suffered from significantly degraded performance and stability. One of the features of 2.2 I haven’t seen detailed anywhere else is the display of street blocks, e.g if you look at 1st and Washington in Seattle, it will show you that it’s in the 200s. Street view is nice, but super slow on EDGE. It’s a little hard to get in to - you have to drop a pin, then press an icon of a head and torso. Existing pins which you’ve dropped have the icon, but it’s not active, which is strange. I like the stop/reload button redesign in Safari. I don’t like the search field eating up space in my location bar at all. I wish I could completely disable it.

More tough love from PHP

Consider the following: class Foo { private $var = 'avalue'; private function doStuff() { } } As you might expect, you can’t access $var or doStuff() from outside Foo, which is well and good. However, things get strange when you throw overloading into the mix. class Foo { private $var = 'avalue'; private function doStuff() { } public function __get($var) { return $this->$var; } } $foo = new Foo; var_dump($foo->var); This works; if a variable isn’t accessible from the calling scope, it will invoke __get() instead. Too bad it’s not consistent: class Foo { private $var = 'avalue'; private function doStuff() { } public function __get($var) { return $this->$var; } public function __call($func, array $args = array()) { return call_user_func_array(array($this, $func), $args); } } $foo = new Foo; var_dump($foo->doStuff()); This breaks with: “Fatal error: Call to private method Foo::doStuff() from context ”.”

Oh my god, PHP.

For reasons I don’t understand, this does not work (ReflectionException ‘Class Foo does not have a property named prop’): class Foo { protected static $prop = 'My value'; } $ref = new ReflectionClass('Foo'); $prop = $ref->getStaticPropertyValue('prop'); However, this does: $ref = new ReflectionClass('Foo'); $props = $ref->getStaticProperties(); $prop = $props['prop']; This does not work; PHP cannot dereference array return values: $props = $ref->getStaticProperties()['prop']; This does not work; PHP cannot dereference objects from new instances: $props = new ReflectionClass('Foo')->getStaticProperties();

Why Emacs is the only editor I can use

Alex Payne – who I sincerely hope does not become a regular foil – agonizes about Mac users using “old text editors.” Phil Hagelberg responds. My 2¢: What I demand in an editor, in rough order of necessity, is:
  1. Syntax highlighting for a wide variety of programming languages and file formats.
  2. Tight integration with source code management, preferably supporting multiple systems.
  3. Transparent access to local and remote files.
  4. An uncluttered user interface. The exact opposite of this.
I have found no other editor which can compete with Emacs for those features. There are a couple that come close. TextMate lacks remote file access; BBEdit is also close, but the cost is off-putting.

Apple’s new laptops: Ugly, unusable crap

I’m sure you couldn’t tell, but I’m not happy about Apple’s announcements today.

They’re Ugly

Apple’s new MacBook/MacBook Pro

Apple’s new MacBook/MacBook Pro

Really. These things just look bad. I hated the nasty black bezel on the aluminum iMac, and I hate it on these. It hasn’t grown on me. They’re plain ugly, eschewing clean minimalism for a terrible retro throwback design. I mean, we’ve been here before. What’s next, Snow White?

Remote iTunes Sharing

I keep most of my media - music and photos - on a 250gb external WDC Passport drive. I usually bring it with me when I come in to the office, but I forgot it today. Whoops. So I set about figuring out how to access it remotely.

The Ingredients

iTunes needs two things to access shared music.

  1. A share present on the local network. Which is to say, on one of the networks the machine is directly connnected to.
  2. An entry for that share in Bonjour.

If you lack either of those, it doesn’t work. Fortunately, it’s not hard to trick iTunes into letting you access any share you want. It’s unfortunate, really. If you use Back To My Mac, any Bonjour services - iTunes included - get announced on your local end, but iTunes ignores it because it’s not on the local network.

Fixing php-mode’s defun handling

One of the things which I’ve been frustrated with when coding PHP in Emacs is the uselessness of it’s defun-handling functions. Things like mark-defun, narrow-to-defun, beginning-of-defun just didn’t work for me. More precisely, they didn’t work when I was writing OOP code, though they worked for procedural code. Not helpful, since I’m a firm believer in object-oriented programming.

The core of the problem is php-beginning-of-defun-regexp, which doesn’t match many of the keywords available since PHP 5: public, protected, private, static, abstract, or final. I toyed with changing it to match all that, but the number of combinations made it somewhat convoluted. Instead, I went with the rather graceless (though easy) regexp you will find below. It simply ignores anything on the line preceding the function keyword.

(defconst php-beginning-of-defun-regexp
   "^.*function\\s +&?\\(\\(\\sw\\|\\s_\\)+\\)\\s *(")

With this, I’m happily using these functions while writing OOP PHP code.

Syndicate content
Copyright (c) 2008 Gongzine