for the people who aren’t up on the ECMAScript standard, ToPrimitive, ToString, and ToNumeric are abstract operations defined in the standard, not functions callable from within Javascript code
compare
({ false: "good", true: "bad" }[{
toString () { return "false" },
valueOf () { return true },
}])
it’s definitely a ⁜little⁜ funky because it calls ToString on ⁜the resulting primitive⁜ not on the original value so
"that is " + {
toString () { return "false" },
valueOf () { return true },
}
returns "that is true"
it just calls ToPrimitive on both sides and then :—
• if the result on either side is a string, calls ToString on both sides and concatenates them
• otherwise, it calls ToNumeric on both sides and attempts to add them
@noelle (strictly speaking javascript operators can throw errors if .valueOf() or .toString() also throw errors or ultimately fail to produce primitives, but that’s not the case for any builtin thing i don’t think. maybe some of the newer ones? i’d have to check)
@noelle “every operator should return a result without throwing an error” is definitely A Choice and they sure made it
@noelle anyway i think the reason why javascript gets a weird reputation isn’t because it lets you do weird typecasting things but because it lets you do weird typecasting things in very very few characters 😝
@noelle which is why personally i don’t like Python (because i find the “everything is an object” approach to be much simpler to reason about than whatever it is Python does) but i understand how for other people, the incredibly ambiguous mess which is JavaScript would be bothersome lol
@noelle i mean i get it but it is indicative of a fundamentally different understanding of []
in JS, [] is just a special kind of object, and all objects are truthy
Python doesn’t take the same “everything is an object™” approach so you wind up with more exceptional cases like that
@noelle what makes this especially scary is that this is equivalent to "[]True"[2]
like, what??
@noelle that’s definitely weird behaviour, but it falls out of the fact that strings and numbers are the only valid outputs of an addition operation, and having `[] + $` be a number is almost definitely worse
@noelle idk `![]` being false seems expected to me, and `false + false` being 0 makes more sense than it being anything else, so the only part of this which i think might be objectionable is the fact that `[] + $` is a string
@djsundog honestly even just like
custom columns based on extended file attributes
i can write a script to set those myself if needbe just let me sort over them please
@alexandra scheme or relatives is a good choice however…
my personal opinion is that while functional programming concepts are good and extremely useful, they are good and useful only for a subset of problems that one might encounter when writing a program…
and more generalist languages have enough support for functional programming these days that they’re generally more pleasant to work with (unless your problems happen to be exclusively those that functional programming is good at).
definitely if your interest is just in transforming data, learn XSLT or Scheme!
otherwise, if you’re already familiar with Javascript, i’d recommend just doing functional stuff in Javascript+Deno… the caveat here is that the JS community is a mess, and it can be hard to get off on the right foot if you don’t already understand the language.
Swift is a very good programming language for functional programming in my experience. it’s probably my favourite language right now in general, although perhaps less because of its strengths and more because of the failings of the rest of them 😝.
Administrator / Public Relations for GlitchCat. Not actually glitchy, nor a cat. I wrote the rules for this instance.
“Constitutionally incapable of not going hard” — @aescling
“Fedi Cassandra” – @Satsuma
I HAVE EXPERIENCE IN THINGS. YOU CAN JUST @ ME.
I work for a library but I post about Zelda fanfiction.
For the time being, this is mostly a mirror of <https://status.ladys.computer/>. Want to get in touch? E·mail me!