While hacking on some Ruby code today I started to miss
progn from my Common Lisp programming days. If you’re not familiar with
progn, it’s a special form which evaluates all of the contained expressions and returns the value of the last one.
progn construct is more important in the (more-or-less) functional1 programming world than in the imperative world because it allows us to insert multiple expressions where, syntactically, we could otherwise only insert one.
Let’s start with a trivial example:
(if (> x 0) x 0)
x is greater than zero, return
x, else return 0.
But what if we also wanted to output a message whenever
x is not greater than 0? We can’t just add our printing line in before (or after)
0 because the compiler knows what’s what based on the positions of the subexpressions in the
if expression: it has to be
(if test-form then-form [else-form]) 2.
(if (> x 0) x (progn (print "Returning zero!") 0))
Boom! It works. How very exciting. Ok, maybe not. In fact, I called
progn a “special form” earlier—which basically means that it’s something which defies the basic evaluation rule of the language—but the truth is it’s probably the least special of the special forms. In fact, implicit progns are all over the place in Lisp:
let forms, but most importantly, function bodies.
When dealing with pasted or dragged data in Cocoa, we receive the passed data on an
NSPasteboard. Prior to v10.6 (Snow Leopard), this pasteboard could only contain a single item, but now it can contain multiple items, which are returned as an
NSArray via the
The data passed in via the pasteboard can be in a variety of types (for instance, a single image could be passed both in JPEG format and TIFF format), and for compatibility reasons these types can be expressed in various ways—they can be MIME types, UTIs, OSTypes, or PboardTypes.
If we want to handle pastes and drags in the proper, modern, multi-item fashion, you’d think we could just iterate over the
-pasteboardItems (each is an
NSPasteboardItem), ask each for its
-types, and handle the data in our preferred format. Unfortunately things aren’t always that simple.