If Shakespeare wrote in JavaScript, here’s what it would look like

Recently I had a dream in which I set homework assignments for Ernest Hemingway and 24 other literary luminaries. Each author received one of five tasks—common coding problems, mostly mathematical—which they were to solve using JavaScript. Naturally this was all too good to keep to myself so I turned their solutions into a book—If Hemingway Wrote JavaScript—excerpted below.

But what does literature have to do with programming? What does the essential distillation of truth and perception have in common with a bunch of instructions for a computer?

While it’s easy to dismiss coding as rote exercise—a matter of following rules—it’s worth remembering that natural language is subject to rules of its own: grammar, syntax, spelling. The best writers test these rules, bend them, or break them outright, and in doing so they keep the language alive. Programmers break rules too. When a good programmer breaks a rule, it’s not for effect—they’re trying to overcome an arbitrary convention that’s hampering their ability to express themselves. Software’s indebted to these programmers who are curious enough to experiment with language, because that’s how new patterns and techniques are conceived. With that in mind, I wanted to apply the quirks and transgressions of the great authors to JavaScript, to see where that pushed the language.

Shakespeare’s Fibonacci Sequence

Screen Shot 2014-10-18 at 5.29.56 PM

Shakespeare’s solution comes in the form of a two-act comedy that draws heavily on JavaScript’s unusual mannerisms for levity. We’re introduced to the cast of players before settling in for the main event. In keeping with the traditions of Elizabethan comedy, the unsettling opening act (in which an incomplete result is prematurely presented) is happily resolved by the final act, affording us much comfort and cheer.

Shakespeare
Shakespeare. (Original artwork by Miran Lipovača.)

The Bard gets a little wordy, but we wouldn’t have it any other way. Several clever devices are employed—for example, the use of Math.max ensures that theResult does not suffer the indignity of being addressed by a negative index.

Notice that although Shakespeare’s comments are in iambic pentameter, he’s using weak endings (that is, adding an extra unstressed syllable). Shakespeare frequently used weak endings to denote enquiry or uncertainty (the Elizabethan equivalent of upspeak). We can only assume he found JavaScript as vexing as the rest of us do.

Borges’s Prime Number Generator

Screen Shot 2014-10-18 at 5.30.38 PM

Borges’s solution combines several of his favorite motifs: mathematical theory, the geometric arrangement of spaces, a suggestion of infinity, and a story within a story. This is classic Borges: a narrator—who, in his excitement, possibly overplays the staircase imagery—tells of a mysterious book (Monsters Ascending a Staircase), and within a few short lines, we’re right there living it.

Borges.
Borges. (Original artwork by Miran Lipovača.)

As we watch the dogged procession of upwardly mobile monsters, we can’t help wondering when Borges will get to the math part. Predictably, it turns out we were already there; the rank of the stair represents the numerator, and each monster’s stride the denominator. Stairs that remain unstomped have no divisors and are thus primes. By transporting us to this imaginary world, Borges creates a distraction from the raw mechanics of prime numbers, while simultaneously illustrating how utterly simple, and universal, they are.

Borges’s logic is clean and well organized, and his JavaScript straightforward and free from unnecessary cleverness, as befits his dislike for syntactic filigree. Yet, by dint of a few well-chosen comments and variable names, he achieves a glorious otherworldly effect.

Sometimes the assignment proved to be too much, and the author would abandon it and instead send me a poem documenting the dark depths of their coding despair.

Here’s Edgar Allan Poe’s reworking of The Raven:

Once upon a midnight dreary, while I struggled with JQuery,
Sighing softly, weak and weary, troubled by my daunting chore,
While I grappled with weak mapping, suddenly a function wrapping
Formed a closure, gently trapping objects that had gone before.

Ah, distinctly I remember, it was while debugging Ember,
As each separate dying member left its host for evermore.
Eagerly I wished the morrow—vainly I had sought to borrow
(From my bookmarked trail of sorrow), APIs from Underscore.

There I sat engaged in guessing the meaning of each cursed expression,
Endless callbacks in procession; nameless functions, nothing more,
This and more I sat divining, strength and spirit fast declining,
Disclose the value we’re assigning! Tell me—tell me, I implore!

Reproduced with permission from No Starch Press. We welcome your comments at ideas@qz.com.

home our picks popular latest obsessions search