• SamiDena@programming.devOP
    link
    fedilink
    arrow-up
    2
    ·
    1 month ago

    Just remember that back when Knuth wrote this, there was no such thing as ‘scripting’. So if you don’t necessarily ‘program’ but ‘script’ a lot, that’s the same. With scripting, the cleverness is not in algorithms you use or stuff like that, it’s as you said, clever use of resources. I have a story to tell:

    A few hours ago my brother showed me this guy on Twitter telling people that, he’s asked people to ‘partition an array of numbers and null on null’ (in JS) and he showed his original solution, which was an iterative solution, very non-functional in style, and I kinda don’t like code that is just “too” imperative you know? Then my brother showed me someone’s solution.

    const arr = [21, 242, 1135, null, 1341, null, 2424, 11, 22, 444 ];
    // solution
    arr.join(',').split(',,').map(subarray => subarray.split(','))
    

    Golfing like this is exactly what would make Knuth cry! I wish people understood that golfing is not very readable! But understanding why this happens is what makes you more endearing, to me at least! This happens because of Javascript’s orthogonality, a remnant of Smalltalk-80 being retrofitted into a prototype-based system! Also, this is because ECMA-262 has specified ‘join’ to ignore ‘null’. Because ‘null’ is not a prototype, it’s baked into the grammar! It has no way to respond to ‘join’ so it gets left out! This is because of how Smalltalk-80-based languages are just “always communicating”. “null” is part of grammar, it does not communicate, so it gets left out.

    Now, knowing that, this code looks more beautiful right?