Matthew Jaquish

Audrey in Functionland: Chapter 1

| Comments

In Which Audrey Searches for Mr. Whiskers

One fine Summer day, Audrey was out and about with her white cat, Mr. Whiskers. They always enjoyed a good wander about the fields, searching for sticks and stones and pleasantly shaded trees to rest beneath(and of course, Mr. Whiskers was also looking for mice, but Audrey didn’t need to know).

1
2
3
4
5
6
var whiskers = {
    name    : 'Mr. Whiskers',
    type    : 'cat',
    color   : 'white',
    isHungry: true
};

After a particularly productive afternoon of collecting enough odds and ends to build a small fairy house against the roots of a rather large Oak tree, Audrey sat down and rested her back against the Oak’s trunk. A moment of quietude descended upon her mind and she drifted into a pleasant state(and of course she didn’t let her eyes close for a bit). Suddenly, she sat upright when it dawned on her that she hadn’t seen Mr. Whiskers in longer than she liked.

She leapt to her feet and began a frantic search amongst the field and along the stream. Just before despairing, she noticed a strange motion out of the corner of her eye. There was a jumble of things moving about. But what kind of things were they?

1
2
3
4
5
6
var whiskersLike = [
    {'name': 'name',     'value': 'Mr. Whiskers'},
    {'name': 'type',     'value': 'cat'         },
    {'name': 'color',    'value': 'white'       },
    {'name': 'isHungry', 'value': true          }
];

“Oh my!” exclaimed Audrey. “You certainly seem like Mr. Whiskers, but you really aren’t the same, are you?”

The jumble of things rustled and shifted, but no answer came to her. Audrey pursed her lips and tapped a cheek with her index finger.

“Well, your name is a name, but your value is a name… it’s all together much too confusing. What can we do about it?”

1
2
3
function nameVal(o) {
    return [o.name, o.value];
}

“This looks like it will rescue the things that are Mr. Whisker’s, but we still need to put them back in a proper way!”

1
2
3
4
5
function object(pairs) {
    var o = {};
    pairs.forEach(function (pair) { o[pair[0]] = pair[1] });
    return o;
}

“I suppose this will let us get things moving in the right direction. However, even if we have the right pieces, we still need to solve the puzzle!”

Audrey set about pulling this and that from the jumble of things and mixing and matching them as fast as she could.

1
var whiskers = object( whiskersLike.map(nameVal) );

When she finished the assembly, she sat down and beheld her handiwork.

1
2
3
4
5
6
{
    name    : 'Mr. Whiskers',
    type    : 'cat',
    color   : 'white',
    isHungry: true
}

Mr. Whiskers let out a little meow and purred while he rubbed against her leg. Audrey scooped him up and gave him a twirling hug.

“Mr. Whiskers, I’m so glad that you are back! You must be hungry, so let’s get back home for tea time!”

Audrey paused for a moment and looked over her shoulder at the giant Oak tree. She wondered what exactly happened in that moment she spent sitting against the trunk. As for Mr. Whiskers, he glanced to and fro for a futile glimpse of a mouse.

Comments