Uncaught Exceptions in Clojure

Uncaught exceptions in Clojure sometimes disappear into the void if they happen in a background thread. Fortunately, the JVM provides a hook for adding a default handler:

   (reify Thread$UncaughtExceptionHandler
     (uncaughtException [this thread throwable]
       ;; do something with the exception here.. log it, for example.
Posted in Tech | Tagged | Comments Off on Uncaught Exceptions in Clojure

Why is NATO in Libya? Data-based analysis of whether it’s just an oil grab.

Why is the US involved in Libya today? Why is NATO there? Why aren’t they doing more or less (depending on your politics) than they are? Is the whole thing yet another evil colonial imperialist plot to make the white man rich by seizing the Other’s oil? Let’s look at some hard data and see what we can figure out. Continue reading

Torrent Contents – List Files in a Torrent

List the files in a BitTorrent torrent with this quick Ruby script. Requires that you gem install bencode awesome_print before you run it.

#!/usr/bin/env ruby
# torrentdump - prints the contents of a torrent.
# Copyright (C) 2011 Paul Legato. All rights reserved.
# Licensed for personal, non-commercial use only.
# This code comes with NO WARRANTY, express or implied.
# Prerequisite gem setup:
#   sudo gem install bencode awesome_print

require 'rubygems'
require 'bencode'
require 'awesome_print'

unless ARGV[0]
  STDERR.puts <<END

Usage: torrentdump <filename> <-v>

If -v is given, prints the entire contents of the torrent except the binary "pieces" hash data.

If -vv is given, prints everything, including the binary hash data.

Otherwise, prints only the file list.

  exit 1

# Method from http://stackoverflow.com/questions/3201890/is-there-an-elegant-way-to-remove-a-specific-key-from-a-hash-and-its-sub-hashes
def recursive_delete!(hash, to_remove)
  hash.each_value do |value|
    recursive_delete!(value, to_remove) if value.is_a? Hash

data = BEncode.load(File.read(ARGV[0]).force_encoding("binary"))

if ARGV[1] == "-vv"
  ap data
elsif ARGV[1] == "-v"
  recursive_delete!(data, "pieces")
  ap data
  if data["info"]["files"]
    dir = data["info"]["name"] || ""
    ap data["info"]["files"].map {|x| dir + "/" + x["path"].join("/") }  
    ap data["info"]["name"]

A quick example:

% torrentdump ubuntu-10.04.2-server-i386.iso.torrent 

Torrent dissection: of interest here..

The Torrentdump source code is available on GitHub.

Posted in Tech | Comments Off on Torrent Contents – List Files in a Torrent

Stop Calling Your Tests “Stories”

Stop calling your tests “stories.” Really.

They’re not “stories”. They’re tests. They don’t tuck you into bed with a glass of warm milk and a nice fairytale to dream about; they assert that certain things should happen and complain when they don’t, in a purely mechanical and precise way. We’re programming computers, machines of pure logic here, not reading to children. Let’s dispense with the pointless cutesy façade and focus precisely on the task at hand.

They don’t deliver what they promise, anyway. Frameworks like RSpec and Cucumber do not let you write tests in “plain English”. They let you write tests in a horrible chimeric programming language that adopts a superficial English-like veneer over top of what’s essentially a regular old unit testing framework.

It winds up giving you the benefits of neither English nor a well written testing API; your code simply becomes excessively verbose for the sake of a (highly fake and brittle) natural language veneer. You’re still highly restricted in what you can say, just like a regular old programming language. The set of Englishisms that you can use is so grossly restricted and precisely defined that you can’t think in anything remotely like English. You have to memorize a precise set of rigid, fixed English labels and use that and only that. You do get the verbosity of natural language, though, along with the restricted semantic domain of the API. You get the benefits of neither and the disadvantages of both.

Since you’re memorizing a highly restricted API anyway, skip the twee cruft and just use a short and precise API from the start, without the pseudo-natural language clutter.

Custom Dialog Boxes Using Cocos2d on iPhone

Custom dialog boxes are a must for any game. iPhone’s standard dialog boxes are fine for a business or productivity app, but in a game they look out of place. Users play games to escape from their everyday lives. Showing them the same exact graphic design that they associate with work and ordinary routine is a great way to subtly interrupt the immersiveness of their gameplay experience. We don’t want that, so we need to customize.

iPhone UIAlertView to custom dialog box example. Doesn't the custom one look much better?

Cocos2d makes it easy to convert your drab standard UIAlertViews into glorious customized color dialog boxes. Above is an actual UIAlertView / custom dialog box transition from my upcoming iPhone game. Here’s how I did it. Continue reading