Angus Croll

Twitter Engineer, JavaScript Obsessive, Literature Junkie. Wrote that Hemingway thing.

Read this first

Salinger, Nabokov and Tupac do JavaScript

(New Book “If Hemingway wrote JavaScript” Now Available)

So anyway, I asked J.D. Salinger, Geoffrey Chaucer, Tupac Shakur, Virginia Woolf and Vladimir Nabokov to solve the happy numbers problem in JavaScript. Amazingly they all responded. Here’s how they did…

The Problem

A happy number is one which, when repeatedly replaced by the sum of the squares of its digits, will tend towards 1. By way of reference here’s a bog-standard solution:

function isHappy(n) {
  // to avoid infinite looping, keep a history of numbers already visited
  var history = {'1': true}, nextNum;
  while( !history.hasOwnProperty(nextNum) ) {
    nextNum = 0; // prepare to calculate the next number in the series
    history[n] = true;
    // split the number into digits and square each one
    n.toString().split('').map(function(digit) {
      return digit * digit; 
    }).forEach(function(squaredDigit) {
...

Continue reading →


Macbeth’s lost callback

Following another test failure, Macbeth ponders his pitiful code. In desperation he implores the overlooked callback to invoke itself.

SEYTON

The tests, my lord, have failed.

MACBETH

I should have used a promise;

There would have been an object ready made.

Tomorrow, and tomorrow, and tomorrow,

Loops o'er this petty code in endless mire,

To the last iteration of recorded time;

And all our tests have long since found

Their way to dusty death. Shout, shout, brief handle!

Thine’s but a ghoulish shadow, an empty layer

That waits in vain to play upon this stage;

And then is lost, ignored. Yours is a tale

Told by an idiot, full of orphaned logic

Signifying nothing.

————– ❦ ————–

Original text:

SEYTON

The queen, my lord, is dead.

MACBETH

She should have died hereafter;

There would have been a time for such a word.

Tomorrow, and tomorrow, and tomorrow,

Creeps in this petty pace from
...

Continue reading →


If Kerouac wrote JavaScript (and Dr Johnson wrote CoffeeScript)

(New Book “If Hemingway wrote JavaScript” Now Available)

Last summer I introduced the concept of literary JavaScript with If Hemingway wrote JavaScript in which five well known authors wrote a JavaScript utility to generate the Fibonacci sequence. In May I presented their efforts at JSConf 2013, and to mark the occasion I asked an additional six authors (as well as the irrepressible Hemingway) to solve factorial(n) as only they could. Here’s what they sent me…

Jack Kerouac

“All of JavaScript is a foreign country”

/*...the only numbers for me are the mad ones, take forty-three like a steam engine with a talky caboose at the end*/ n = 43, /*and that lanky fellow in a cocked fedora*/ r = 1 /*then back to our number, our mad number, mad to become one*/ while (n > 1) /*mad to descend*/ n--, /*mad to multiply*/ r = r * n /*and at the end, you see the blue center-light pop, and everybody
...

Continue reading →


If Edgar Allan Poe wrote JavaScript

(New Book “If Hemingway wrote JavaScript” Now Available)

(after 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 ever more.

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!

Continue reading →


How to Read a Book

Although Ernest Hemingway’s brilliant 1927 story Hills like White Elephants is only three pages long, there are tens of thousands of pages of academic analysis telling us how to read it.

The story itself is little more than tense, disjointed exchange between a man and a woman at a railway station somewhere between Barcelona and Madrid. Instinctive, melodic, enigmatic, irrefutable—the dialog is all we have, and it’s all we need.

I have tried to eliminate everything unnecessary to conveying experience to the reader so that after he or she has read something it will become a part of his or her experience and seem actually to have happened. — Ernest Hemingway

Hemingway gives us the bare minimum so that we might subconsciously fill the gaps with the logic and imagery of our own existence; he surely didn’t intend that blustering pedagogues and glorified cheat sheets should do it for us...

Continue reading →


The Oxford Comma and The Internet

The Oxford comma (also known as the serial comma) is that extra comma that you sometimes get at the end of a list, before the and or the or. “She wrote novels, essays, and JavaScript” uses an Oxford comma. “He bought apples, butter and the ranch” doesn’t.

The Oxford moniker derives from the century-old endorsement of the serial comma by the Oxford University Press manual of style; and the OUP is backed up by a slew of revered authorities: Strunk’s Elements of Style, Fowler’s Dictionary of Modern English Usage and the Chicago Manual of Style. Why? Because omitting the Oxford comma can result in distressing double meanings:

“She lives with her two children, a cat and a dog.”

Legions of grammarians are quick to point out that while the lack of an Oxford comma can cause ambiguities, its presence never will. Here’s something we can all get behind, right?

Well, no, not really, because...

Continue reading →


Animals writing JavaScript

In 1886, French composer Camille Saint-Saëns premiered his orchestral suite The Carnival of The Animals. Although each of its 14 movements was ostensibly a musical representation of an animal, several movements were rumored to be veiled descriptions of Saint-Saëns’ human acquaintances (and perhaps even of himself). In the spirit of Carnival of The Animals, I recently embarked on a comprehensive survey of our furry friends and their approaches to JavaScript. Here’s what I discovered.


Donkey equus asinine

The donkey is a sad, ungainly creature. He really doesn’t want to be doing this. Forlornly, he logs in for yet another humdrum day of struggling with this silly, upstart language which doesn’t even have proper types. Oh the futility of it all.

Mule equus obstinatus

Mules are as doleful as donkeys but stubborn to boot. Mules don’t forget, and will doggedly persist with patterns...

Continue reading →