🐾 rigby

GRIBFILE #1.5: divine intervention

It was sometime in the waking hours of the morning, when all else was still at rest. The sun had not yet shown his face to the flat earth (yes, globeheads, it’s flat. Deal with it), and all was still. All except for one small dingo celebrating his victory. It had been finished. The AST generator had been completed. Everything from top to bottom of the sloppily-indented GRIB sample file had been properly lexed and parsed. The printed AST was all squished onto one line in the console and barely readable, but it was a beautiful sight to Budster’s little beady eyes. \“All right then, I should probably go to bed\”, said Budster as he happily looked over at the ball of sheets that rested atop his bed. Budster had a system of sorts: instead of making his bed, he could just crawl inside the sphere of covers. That way, the covers surrounded him and he didn’t even have to invest five minutes into making his bed, creating a win-win situation. Win-win situations like these were why Budster woke up every morning. He turned off the lights and crawled in his cocoon of blankets. He shut his eyes and tried to go to sleep, but his efforts proved to be fruitless. A solid 45 minutes had passed, and the sleep didn’t come. The fan hummed as it always did, and something felt off. Budster sensed something, something powerful. Something important. Budster thought he could hear a mumbling noise just beneath the droning of the electric fan. At first the murmurs were hard to decipher, but the noise became louder and clearer the more he listened. \“PROPER FUNCTIONS SHOULDN’T CAPTURE OUTER STATE. PROPER FUNCTIONS SHOULDN’T CAPTURE OUTER STATE\”, professed the voice. The chanting continued. \“PROPER FUNCTIONS SHOULDN’T CAPTURE OUTER STATE. PROPER FUNCTIONS SHOULDN’T CAPTURE OUTER STATE\”. The voice stopped. Moments later, a loud popping noise sounded and a giant crab appeared before the bed. Budster got up and began trembling. He collapsed into a bowing position.
\“Ferris, I am not worthy that you should enter under my roof, but only say the word and my borrows shall be checked\”. Ferris adjusted his pincers. \“My dear Budster, your compilation time has not yet come. I bring news concerning your quest.\” Budster’s eyes lit up.
\“What news do you bring?\”, inquired the dingo. His head cocked to the side a bit as he asked this. Ferris paused a bit, almost as if he were trying to reword a scathing remark in a more friendly way.
\“Come closer, my child\”, said the crab in the form of an affectionate fatherly whisper. Budster crawled toward the holy crustacean and sat at his legs. The crab came close to Budster’s ear and whispered the following message: \“GRIB sucks\”. Budster stood still for a while. The first stage of grief had stricken him: denial.
\“P-Pardon me?\”, the dingo boy stuttered. Ferris donned a pensive look on his giant crab face. \“Child, I’m afraid you’re creating an unholy abomination. I can’t word this any other way\”. Budster tried holding back the tears. He tried defying the overwhelming sensation of defeat he was drowning in. He tried to ignore the loud clammer of his very soul as it shattered into a thousand jagged pieces. He tried to ignore his invisible dad, who was saying, \“Wow, I can’t believe you shattered your soul. Get the dustpan and sweep that stuff up before someone cuts their foot\”. Ferris, the one chosen by He Who Told Us To Stop Talking About That, had just spurned Budster’s baby.
\“Why, Ferris? Why must you do this? You kept me from writing unsafe code for so long. But now, you lash out at me and torment me. How could you?\” cried Budster.
\“Now, now. Please don’t cry. I didn’t come to state the obvious, I came to point you in a better direction\”, said the crab in the form of a gentle whisper, \“Could you show me a Grib iterator factory implementation so we can begin sinking our pincers into this?\”. Budster pulled out a piece of piece of paper from his dingo pocket that contained Grib code written in Times New Roman and handed it to the holy crab. It looked like this:

proc newIter {
    decl arr = args[0];
    decl index = 0;
    
    proc next {
        decl val = arr[index];
        index += 1;
        return val;
    }
    
    proc hasNext {
        return index < arrayLength(arr);
    }
    
    return [
        next,
        hasNext,
    ];
}

proc iterNext {
    return args[0][0]();
}

proc iterHasNext {
    return args[0][1]();
}

decl iter = newIter([1, 2, 3]);

while iterHasNext(iter) {
    println(iterNext(iter));
}

Ferris’s gentle smile faded. \“Oh dear,\” said the crab, \“There are so many things that are wrong with this and I simply don’t know where to begin.\”
Budster desperately tried to hold back his tears and salvage any scraps of dignity he had left.
\“Ok, what’s up with this args thing. How on earth did you think this was acceptable?\” cried Ferris.
\“W-well it’s good enough for Perl\”, replied the dingo boy. Ferris was fuming, but he appeared to be making a concerted effort to conceal his ire.
\“You do realize that this was the reason you were put onto this earth, right? Don’t you understand the obligation you took up when you exited the womb?\”, said Ferris as he leaned closer to Budster, \“Sometimes ‘good enough for Perl’ just doesn’t cut it. So here’s the deal: your job is to get rid of that stupid args construct and give your functions real parameters. It’s clear that you’ll need some serious guidance, so allow me to explain my plans\”. The crab began etching a visual on Budster’s wooden floor. \“God damn it\”, remarked Budster’s invisible dad, \“that floor got redone two weeks ago!\” After several seconds of busy scratching, the crab stood back and gestured toward his work. \“It’s certainly nowhere near perfect, but it will have to do\”, said the crab.

proc newIter |arr| {
    decl index = 0;
    
    return # {
        next -> lam {
            decl val = arr[index];
            index += 1;
            return val;
        },
        hasNext { 
            get || { index < arr.len() }
        }
    };
}

decl iter = newIterator([1, 2, 3]);

while iter.hasNext {
    println(iter.next());
}

\“You see\”, Ferris continued, \“Our factory functions would consist of regular, exportable functions. These functions would return ‘hashes’ full of lambdas, getters, and setters. Internal state wouldn’t be exposed via the returned hash. These hashes would be more like hash maps than objects. There wouldn’t be any sort of inheritance. I’d like to emphasize that regular functions CAN NOT capture outer state. They can only call other regular functions in scope. Lambdas should be the ‘functions’ that capture outer state. This certainly isn’t going to make Grib the next Rust, but I think it could possibly save the model you were trying to achieve in your provided example.\” Budster stood dumbfounded for a moment and tried to process what he had just heard.
\“Got it?\” asked Ferris. The dingo responded with a blank stare.
\“Good\”, said the crab moments before popped out of sight in the same way he had entered.

Budster had a long journey ahead of him, and his stomach churned with excitement and fear.

Comments

musicalbird on 2019-03-22

you can do it budster, i believe in you!

new reply

new comment