r/badcode • evil • Jan 14 '21

js Dynamically created functions are the future!

Post image
795 Upvotes

102 comments sorted by

View all comments

95

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jan 14 '21

Image Transcription: Code


// this is the compile function that will become our dimension list;
let compileDimensionList = function (properties) {
    let dimensionFunStr = '(function (d) {\n';
    let dimString = '';
    for (var i = 0, len = properties.length; i < len; i++) {
        if (i === (len-1)) { // end of the list
            dimString += 'd.'+properties[i];
        } else { // every otther item
            dimString += 'd.'+properties[i]+' + ';
        }
    }
    dimensionFunStr += 'if (typeof timeLookup['+dimString+'] === "undefined") {\n';
        dimensionFunStr += 'timeLookup['+dimString+'] = d.time;\n';
    dimensionFunStr += '}\n';
    dimensionFunStr += 'if (d.time > timeLookup['+dimString+']) {\n';
        dimensionFunStr += 'timeLookup['+dimString+'] = d.time;\n';
    dimensionFunStr += '} else {\n';
    dimensionFunStr += 'd.time = timeLookup['+dimString+']\n';
    dimensionFunStr += '}\n';
    dimensionFunStr += 'return '+dimString+';\n';
    dimensionFunStr += '})';
    return eval(dimensionFunStr);
}
let dimensionList = compileDimensionList(groupby); // compile dimension functions

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

4

u/[deleted] Jan 15 '21

[removed] — view removed comment

4

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jan 15 '21

Unfortunately, OCR isn't currently at a stage where it can be used for this purposes.

We actually have a bot that attempts to transcribe posts. It's pretty much the best OCR not behind an expensive paywall, and while it does mostly okay with simple posts that are basically just text, it's easily messed up by anything from different fonts to weird formatting.

One of the ones it has the biggest problem with is code like this. For some reason it just always messes up all of the formatting, and skips over many lines and characters. Some people use its attempts as a basis they correct, but I find to be quicker to just transcribe the post from scratch.

I agree; it would be great if an advancement could be made in the field, but currently, the technology's not at a stage where it can be putting out official transcriptions that haven't at least been vetted by a human, and in the case of code it is, as I've said, useless.

Hope that clears it up! If you have any further questions I can do my best to answer them!