95
class BaseFunction {
  static #allowInstantiation = false;

  constructor(...args) {
    if (!BaseFunction.#allowInstantiation) {
      throw new Error(
        "Why are you trying to use 'new'? Classes are so 2015! Use our fancy 'run' method instead!"
      );
    }
    for (const [name, validator] of this.parameters()) {
      this[name] = validator(args.shift());
    }
  }

  parameters() {
    return [];
  }

  body() {
    return undefined;
  }

  static run(...args) {
    BaseFunction.#allowInstantiation = true;
    const instance = new this(...args);
    BaseFunction.#allowInstantiation = false;
    return instance.body();
  }
}

class Add extends BaseFunction {
  parameters() {
    return [
      ["a", (x) => Number(x)],
      ["b", (x) => Number(x)],
    ];
  }

  body() {
    return this.a + this.b;
  }
}

console.log(Add.run(5, 3)); // 8



top 29 comments
sorted by: hot top controversial new old
[-] CrossbarSwitch@lemm.ee 1 points 9 hours ago

I've seen something similar to this at work. Horrible.

[-] NigelFrobisher@aussie.zone 2 points 12 hours ago
[-] FiskFisk33@startrek.website 3 points 20 hours ago

That'll be fun in a multi threaded setting!

[-] TunaSlap@lemmy.world 3 points 23 hours ago

What theme are you using, i like it!

[-] Chais@sh.itjust.works 4 points 22 hours ago

Looks like Catppuccin Mocha

[-] TunaSlap@lemmy.world 2 points 21 hours ago

Actually now that check it again its not quite right for mocha. But it's close!

[-] Chais@sh.itjust.works 2 points 21 hours ago

You're right, the background is too dark. Probably crust instead of base. Maybe it was customised or created improperly.
But I'm fairly confident that the palette is Catppuccin, probably Mocha.

[-] sebastiancarlos@lemmy.sdf.org 2 points 20 hours ago* (last edited 20 hours ago)

I can confirm it's Catppuccin Mocha. I am not currently aware of the background color issue, but I'll look into the matter soon. Thanks for letting me know. Also how dare you. But thanks.

[-] Chais@sh.itjust.works 1 points 14 hours ago

The background is most likely a color that is in the Mocha palette, just one that is intended for dark accents, not regular background.

[-] TunaSlap@lemmy.world 2 points 21 hours ago
[-] bleistift2@sopuli.xyz 15 points 1 day ago

validators is a shitty name for something that actually does type conversion.

[-] PanArab@lemmy.ml 10 points 1 day ago

This should be programmer horror

[-] pfm@scribe.disroot.org 21 points 2 days ago

A true FP programmer would make it apply instead of run...

[-] xmunk@sh.itjust.works 8 points 1 day ago

Ahem, map...

And, of course, everything is a lazy list even if the functions can't handle more than one element in each list.

[-] Burghler@sh.itjust.works 7 points 1 day ago

JS disgusts me

[-] BaumGeist@lemmy.ml 24 points 2 days ago

OP, what's your address? I have a "present" for you

[-] navi@lemmy.tespia.org 9 points 2 days ago

Dont look at C++ with std:: function

[-] NorthWestWind@lemmy.world 12 points 2 days ago
[-] ornery_chemist@mander.xyz 5 points 1 day ago

Hence, Clojure. It's not just functions that implement IFn... as the string of "cannot cast to clojure.lang.IFn" errors that I get because I couldn't be bothered to validate my data's shape is eager to inform me.

[-] Ephera@lemmy.ml 12 points 2 days ago* (last edited 2 days ago)

Yep, some code examples from the official documentation. This:

printPersons(
    roster,
    (Person p) -> p.getGender() == Person.Sex.MALE
        && p.getAge() >= 18
        && p.getAge() <= 25
);

...is syntactic sugar for this:

interface CheckPerson {
    boolean test(Person p);
}

printPersons(
    roster,
    new CheckPerson() {
        public boolean test(Person p) {
            return p.getGender() == Person.Sex.MALE
                && p.getAge() >= 18
                && p.getAge() <= 25;
        }
    }
);

...which is syntactic sugar for this:

interface CheckPerson {
    boolean test(Person p);
}

class CheckPersonEligibleForSelectiveService implements CheckPerson {
    public boolean test(Person p) {
        return p.gender == Person.Sex.MALE &&
            p.getAge() >= 18 &&
            p.getAge() <= 25;
    }
}

printPersons(roster, new CheckPersonEligibleForSelectiveService());

The printPersons function looks like this:

public static void printPersons(List<Person> roster, CheckPerson tester) {
    for (Person p : roster) {
        if (tester.test(p)) {
            p.printPerson();
        }
    }
}

Basically, if you accept a parameter that implements an interface with only one method (CheckPerson), then your caller can provide you an object like that by using the lambda syntax from the first example.

They had to retrofit lambdas into the language, and they sure chose the one hammer that the language has.

Source: https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

[-] mbtrhcs@feddit.org 8 points 1 day ago

That's not quite right. In bytecode, lambdas are significantly more efficient than anonymous class instances. So while the lambda implementation is semantically equivalent, characterizing it like you have is reductive and a bit misleading.

[-] LodeMike@lemmy.today 0 points 2 days ago* (last edited 2 days ago)

Golang also does this, but it's not classes.

[-] firelizzard@programming.dev 0 points 1 day ago* (last edited 1 day ago)
[-] LodeMike@lemmy.today 1 points 1 day ago* (last edited 6 hours ago)

Golang uses modules, not classes. Each of which may have its own main function.

[-] firelizzard@programming.dev 1 points 1 day ago

Huh? Main file? Do you mean main package? A module can contain an arbitrary number of main packages but I don’t see how that has anything to do with this post. Also are you saying modules are equivalent to classes? That may be the strangest take I’ve ever heard about Go.

[-] LodeMike@lemmy.today 1 points 1 day ago

I meant main function. Oops

[-] NewDark@hexbear.net 6 points 2 days ago

I'm pretty sure this post is designed to kill the soul. I am made slightly worse for witnessing this abortion of an implementation and I will never be quite the same again.

[-] Chais@sh.itjust.works 1 points 2 days ago

I think that's called a functor.

Amazing, lol

this post was submitted on 17 Nov 2024
95 points (93.6% liked)

Programmer Humor

32495 readers
456 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS