this post was submitted on 17 Nov 2024
118 points (96.1% liked)

Programmer Humor

32481 readers
399 users here now

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

Rules:

founded 5 years ago
MODERATORS
 
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 19 comments
sorted by: hot top controversial new old
[–] [email protected] 8 points 6 hours ago

This should be programmer horror

[–] [email protected] 7 points 6 hours ago

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

[–] [email protected] 7 points 10 hours ago

JS disgusts me

[–] [email protected] 20 points 15 hours ago (1 children)

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

[–] [email protected] 4 points 9 hours 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.

[–] [email protected] 8 points 15 hours ago

Dont look at C++ with std:: function

[–] [email protected] 8 points 16 hours 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.

[–] [email protected] 30 points 22 hours ago

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

[–] [email protected] 3 points 14 hours ago

I think that's called a functor.

[–] [email protected] 13 points 21 hours ago (3 children)
[–] [email protected] 3 points 8 hours 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.

[–] [email protected] 13 points 17 hours ago* (last edited 17 hours ago) (1 children)

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

[–] [email protected] 6 points 8 hours 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.

[–] [email protected] 2 points 18 hours ago* (last edited 18 hours ago) (1 children)

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

[–] [email protected] 1 points 7 hours ago* (last edited 7 hours ago) (1 children)
[–] [email protected] 1 points 7 hours ago (1 children)

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

[–] [email protected] 1 points 51 minutes ago (1 children)

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.

[–] [email protected] 1 points 49 minutes ago

I meant main function. Oops

[–] [email protected] 4 points 22 hours ago

Amazing, lol