elfs: (Default)
[personal profile] elfs

Last night, I went to a coding meet-up where a few of the others were playing a game.  There’s a website called Exercism.IO, where you can download code exercises and submit your answers to a global community of like-minded players.  The others had been at it for a few minutes; I had to take time to install the exercises and begin.  The basic problem, coded in JavaScript, was “Here are seventeen sentences, each marked as being one of four kinds of conversation: statement, question, argument, or silence.  Write a function that successfully matches the expectations of the test case.”


While the others struggled with both regular expressions and multiple if / then / else if ... trees, I went straight for a different solution:


    this.hey = function(input) {
        var tests = [[function(i) { return /^\s*$/.test(i); },                                      "Fine. Be that way!"], 
                     [function(i) { return /[A-Z][A-Z]+.*!$/.test(i) || /^[A-Z\s]+\??$/.test(i); }, "Woah, chill out!"],
                     [function(i) { return /\?$/.test(i);},                                         "Sure."]];
        for(var i = 0, l = tests.length; i < l; i++) {
            if (tests[i][0](input)) return tests[i][1]; };
        return "Whatever."; };

Seven lines. (Yes, my Javascript coding conventions look like Python and Coffee. Whitespace is one of my secret weapons.) Where everyone else had 20-plus lines of comparisons, I went with the brute-force solution. It took four iterations to shake out all the bugs, all 17 tests passed, and it was done.


There are plenty of places where this is silly code; it's basically arbitrary string reduction, and is completely dependent upon the order of the tests, but as the last result says, "Whatever." To me, however, this reduces a lot of if / then statements down to a single if expression in a hand-written map/reduce: map all possible regexes with a single string, returning [String | Nothing], then reduce down to a single memo of the String. But really, this comes down to a basic rule: wherever you have a list of things to do, see if your language has a proper List with which to contain them.

Date: 2014-08-14 02:09 pm (UTC)
From: [identity profile] pixel39.livejournal.com
Vaguely randomly--know of any Rails developers in the SF area who are looking for employment? The Spouse's company needs more bodies but they're not having much luck finding any, possibly because they are trying to avoid using recruiters...

I said "You should ask Elf, he might know someone" and he said "I don't know Elf, I've only interacted with him on Twitter..." *sigh*
Edited Date: 2014-08-14 02:10 pm (UTC)

Date: 2014-08-14 06:07 pm (UTC)
From: [identity profile] elfs.livejournal.com
No, Rails is way off my radar these days, and I can't think of anyone off-hand down there who's looking. That place has more job openings than people to fill 'em, and I'm getting calls weekly from SF recruiters wondering if I'm willing to relocate.

Sorry. I wish I could be more helpful.

Profile

elfs: (Default)
Elf Sternberg

May 2025

S M T W T F S
    123
45678910
111213141516 17
18192021222324
25262728293031

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 19th, 2025 12:56 am
Powered by Dreamwidth Studios