120
you are viewing a single comment's thread
view the rest of the comments
[-] itsnotlupus@lemmy.world 20 points 1 year ago* (last edited 1 year ago)

One of my guilty pleasures is to rewrite trivial functions to be statements free.

Since I'd be too self-conscious to put those in a PR, I keep those mostly to myself.

For example, here's an XPath wrapper:

const $$$ = (q,d=document,x=d.evaluate(q,d),a=[],n=x.iterateNext()) => n ? (a.push(n), $$$(q,d,x,a)) : a;

Which you can use as $$$("//*[contains(@class, 'post-')]//*[text()[contains(.,'fedilink')]]/../../..") to get an array of matching nodes.

If I was paid to write this, it'd probably look like this instead:

function queryAllXPath(query, doc = document) {
    const array = [];
    const result = doc.evaluate(query, doc);
    let node= result.iterateNext();
    while (node) {
        array.push(node);
        n = result.iterateNext();
    }
    return array;
}

Seriously boring stuff.

Anyway, since var/let/const are statements, I have no choice but to use optional parameters instead, and since loops are statements as well, recursion saves the day.

Would my quality of life improve if the lambda body could be written as => if n then a.push(n), $$$(q,d,x,a) else a ? Obviously, yes.

this post was submitted on 08 Jul 2023
120 points (92.9% liked)

Programmer Humor

19623 readers
1 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS