Showing posts with label coding. Show all posts
Showing posts with label coding. Show all posts

Tuesday, July 23, 2013

Tongue leprosy

I'm proud to say I haven't coded any new functionality in the last week. I've been exclusively redesigning and tinkering with old code. The code is looking a lot better, I think, but there are still design problems I don't feel equipped to solve. I have this one case where I can't figure out an elegant way out of a circular definition. I have a sort of abstract factory class Synchronizer which has two subclasses, ResourceSynchronizer and CollectionSynchronizer. As they are separate AMD modules, the subclasses need to import Syncrhonizer in their "define" statements. But then there's no way for Synchronizer to have a getSynchronizer() method that will return an instance of one of the two subclasses. In order to have such a method, it would need to either import the two subclasses in its define statement, completing the circle, or return a Promise to return a subclass, which would make getSynchronizer() undesirably asynchronous.

Another problem I've run into is when I try to separate out a logical chunk from a bloated module and it turns into a seesaw and goes out of control. I'll be happily migrating things and then realize that I've gradually moved everything into the new module. Then I start to wonder if the separation was a good idea in the first place or if the code is just experiencing separation anxiety. On the other hand, the seesawing also doubles as a sifting process; as I move the code back and forth, it seems like it gets cleaner. Maybe if I do it enough, the code will disappear and everything will still work.

I think something's wrong with my tongue. There's a spot on the left side, roughly a centimeter in diameter, that's behaving like it's been severely burned, producing a numb tingly feeling when under the slightest pressure, and hurting when I bite it as hard as I can. I've studied it in a mirror and I think there might be a tiny alien trying to claw its way out. It looks cracked like parched land, and generally unwholesome. Maybe I'm allergic to something. Maybe it's not my tongue at all and it's actually someone foot with athlete's foot.

Sunday, July 21, 2013

How to make a sandwich in Javascript

Michelle, if you're reading this, start getting ready for our open mic duet. Don't even worry about the set list, I'll take full responsibility for picking the songs I like. Here's some that you might want to learn how to sing:

by Wings:
Let me roll it
Band on the run

by Blue Oyster Cult:
Burning for you
Take me away
Astronomy

by Led Zeppelin:
That's the way
D'yer M'ker
Houses of the Holy

by Dusty Springfield:
Son of a preacher man

by Save Ferris:
Come on Eileen (their cover rocks, you play the power chords, I'll practice the crazy ska rhythm)

by Squeeze:
Tempted

by Bogushevskaya:
Cafe Ekipazh

Let me know which ones you don't absolutely love so I can convince you otherwise. Everyone else go listen to these and imagine how well they'd sound if they sounded slightly worse. Pretty fantastic? Agree.

One and a half more days of freedom and then my favorite girl will be back to torture me. I need to think about what kind of things I can do now that I can't when she's around. Other than tell vicious lies on this blog with impunity, or sleep with other women. I guess I should get all my bad singing, bad drawing, and bad dancing out of the way so she doesn't have to do too much criticizing right after a hard week's rocking in Korea.

And now, a cool trick from today's JavaScript camp. I've used this before, but never in the general form:

function partial(fn) {
var args = [].slice.call(arguments, 1);
return function() {
return fn.apply(null, args.concat(arguments));
};
}

What this allows you to do is precreate functions when you know some arguments ahead of time. So for example, if you have a sandwich function:

function sandwich(bottom, top, middle) {
return {
bottom: bottom,
middle: middle,
top:top
}
}

...and you know a "good" sandwich always has a pancake on the bottom and a pop-tart on top, you can make yourself a shortcut function easily:

var goodSandwich = partial(sandwich, 'pancake', 'pop-tart');

This essentially hardcodes the (bottom, top) set of parameters and gives you back a function that expects only one parameter - 'middle'. So now you can use the goodSandwich function to make sandwiches with different contents but the same shell:

// equivalent to sandwich('pancake', 'pop-tart', 'turkey')
var goodTurkeySandwich = goodSandwich('turkey');

// equivalent to sandwich('pancake', 'pop-tart', 'Mark')
var goodMarkSandwich = goodSandwich('Mark');

// equivalent to sandwich('pancake', 'pop-tart', 'cheeseburger')
var goodCheeseburgerSandwich = goodSandwich('cheeseburger');

// equivalent to sandwich('pancake', 'pop-tart', sandwich('pancake', 'pop-tart', 'cheeseburger'))
var doubleDecker = goodSandwich(goodSandwich('cheeseburger'));

Mm, a cheeseburger wrapped in two pancakes and two pop-tarts. That'll get you bulimic in no time.