Here's a simple thought, why can't you multiply, devide subtract a string?
In language design terms here's some examples.
"Hello world" * 2 // ["Hello world", "Hello world"];
"Hello world" - 3 // "Hello wo"
"Hello world" / 3 ["Hel", "lo ", "wor", "ld"];
Umm yeah it's not so easy I suppose to handle remainders and it's a little confusing in some of the cases, what do you think?
Top comments (13)
Are you familiar with languages that allow operator overloading? One big problem is achieving a common understanding of what an operator on a object means. For example, why is "Hello world" * 2 = ["Hello world", "Hello world"] and not "Hello worldHello world"?
As a JavaScript guy, we do have OO but its not well known and a bit of a hack. Although it looks cool, it's exotic, so I will never use the technique for fear of other developers scratching thier heads... That being said a plus operator against a string does have quite a bit of terrible type coercion potentials, all very confusing. I suppose the only way to avoid this is to have specifications, OO is not in the spec for JavaScript
JavaScript is truly object-oriented - you can create "objects" directly without the help of constructor functions or classes. JavaScript doesn't implement class-based object orientation like mainstream OOP languages (which are essentially class-oriented). JavaScript classes are a template for creating objects. (Though in some ways JavaScript is also function-oriented)
Function overloading isn't an OO feature. For example Erlang (a functional language) allows the reuse of a function name as long as the arity (number of accepted arguments) varies. And in C++ you can overload non-member functions so procedural code can use overloaded functions (same name but the parameter type signature varies). Operator overloading tends to be an extension of function overloading.
OO oporator overloading
Not OOP
dev.to/adam_cyclones/oporator-over...
In Python, you can do
"string" * 3
but the result is"stringstringstring"
.In Kotlin for example you can do extension function that overloads operator on string but I would not recommend it because it's not very intuitive.
That's cool, what about python integer multiply π€ maybe it's not such a crazy thought.
(I must learn kotlin soon)
While I sometimes use it in Python for delimiters in
print("-" * 100)
I don't think it's a big feature worth to spread. I think the method"*".repeat(100)
would be more clear for people.so we have
so
Ok Now lets multiply the result with 4 to see if we can get the the first value
Right?
Although I am not sure about what your new overloaded operation does, but the point is we can overload operators but first we need to know what that means. what does "*" really means for an array, or what are we talking about when we say "Hello world" can divide by 3.15?
My head, oh my head hurts π
So while the operator versions have brevity on their side I think they lack clarity, especially for those who are encountering this type of usage for the very first time. And in JavaScript the operators don't fit with the way type coercion is designed to work.
The solution to many problems is simply:
On a personal level I've never understood Haskell's obsession with "operators". "operators" don't add anything if they do exactly the same work as a function with one or two arguments - i.e. just stick to a function. The case is different if and only if not all arguments of the operator expression are evaluated, e.g. the conditional operator.
In many ways I prefer Erlang's philosophy:
(Erlang provides the primitives, OTP implements the mechanisms)
i.e. let the language only supply the essential functionality needed to build everything else.
In the case of JavaScript it's a bit different as you are trying to delegate as much work as possible to the browser - the runtime can accomplish many natively implemented tasks much faster.
So before I start:
result = Array.from({ length: 2 }, (_v) => 'Hello world');
The callback is actually slower then map new Array(n). Completely irrelevant off topic stuff right here π
I take the point π― functional is clear and my thoughts ... Are usually a jumbled mess, non the less apparently Python can multiply a string so it must be a good idea, right? π€·ββοΈ
My immediate thought on multiplication was that the result should be "Hello worldHello world", I guess that is why :D
Exactly so, it's so open to interpretation that it can't be implemented successfuly... Except apparently python can do it π