Real Programmers Implement Functions On Top Of Classes
Real Programmers Implement Functions On Top Of Classes
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
Lucy :3 @feddit.org "Why are you trying to use 'new'? Classes are so 2015! [...]"
Uses new to throw error
2 0 ReplyBaumGeist @lemmy.ml OP, what's your address? I have a "present" for you
38 0 Replypfm @scribe.disroot.org A true FP programmer would make it
apply
instead ofrun
...31 0 Replyxmunk @sh.itjust.works Ahem, map...
And, of course, everything is a lazy list even if the functions can't handle more than one element in each list.
14 2 Reply
NorthWestWind @lemmy.world It's just Java
21 2 ReplyEphera @lemmy.ml 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
22 1 Replymbtrhcs @feddit.org 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.
15 0 Reply
ornery_chemist @mander.xyz 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.
4 0 ReplyKairos @lemmy.today Golang also does this, but it's not classes.
3 0 ReplyEthan @programming.dev How so?
1 0 Reply
bleistift2 @sopuli.xyz validators
is a shitty name for something that actually does type conversion.17 0 Replynavi @lemmy.tespia.org Dont look at C++ with std:: function
12 0 Replyburghler @sh.itjust.works JS disgusts me
11 0 ReplyFiskFisk33 @startrek.website That'll be fun in a multi threaded setting!
5 0 ReplyTunaSlap @lemmy.world What theme are you using, i like it!
4 0 ReplyChais @sh.itjust.works Looks like Catppuccin Mocha
5 0 ReplyTunaSlap @lemmy.world Thanks!
2 0 ReplyTunaSlap @lemmy.world Actually now that check it again its not quite right for mocha. But it's close!
2 0 Reply
FourPacketsOfPeanuts @lemmy.world Amazing, lol
4 0 ReplyChais @sh.itjust.works I think that's called a functor.
4 1 ReplyCrossbarSwitch @lemm.ee I've seen something similar to this at work. Horrible.
3 0 ReplyNigelFrobisher @aussie.zone Very DRY.
3 0 Reply