Monday, May 5, 2008

Satire Development Kit: Democrats Use Groovy To Pick Candidate

This is our first SDK: a Satire Development Kit that allows you, dear reader, to create your own satire, tailored to your own taste.

In this case: political taste. And we'll will learn a bit of Groovy along the way.

Architecture

The SDK is based on these steps:

  • No matter where you may lie on the political spectrum, you may agree that the process to pick the next Democratic presidential candidate (in the USA) is complex. It involves a mix of delegates and super-delegates.
  • Groovy is apolitical, but uses delegates to resolve references in closures. Since delegates may be objects, they may be derived from other objects, which we'll dub super-delegates.
  • Punchline: this is up to you. We just provide the tools.

Minimalist Example


class Foo {
def bar() { println "hello" }
}

def c = { bar(); }
c.delegate = new Foo() // delegate is a keyword

def candidates = [ "Clinton", "Obama" ]
candidates.each( c )

In the above example:
  • candidates is an array that has a closure, c, applied to each element
  • the closure calls bar()
  • Groovy will search for bar(), first in the owning scope of c (the script itself), and then, if necessary, in c's delegate
  • Groovy will not find bar() in the owning scope, but does find it in the delegate, which we assigned as a Foo

Full Example

Below, we have a full example where a self-named Delegate object acts as the, well, delegate and derives from the SuperDelegate, which can pledge (or not?) for a candidate.

class SuperDelegate {
def clintonPunchline = "TODO: fill in"
def obamaPunchline = "TODO: fill in"

def pledge(def candidate) {
if( candidate == "Clinton" ) {
println clintonPunchline
} else if ( candidate == "Obama" ) {
println obamaPunchline
}
}
}

class Delegate extends SuperDelegate {
// defer to base class
}

def convention = { it -> pledge(it); }
convention.delegate = new Delegate()

def candidates = [ "Clinton", "Obama" ]
candidates.each( convention )

The Upshot

Simply fill in the punchlines in the above Groovy script, and you're all set to lampoon (or support) one of the Democratic nominees -- or both -- via a dynamic language on the JVM. The direction of your satire is up to you. Impress friends and family no matter if you are a Democrat, Republican, independent, or international observer.

Enjoy!

1 comment:

Unknown said...

actually, it should be
class SuperDelegate extends Delegate