5 ms·
Sadly I agree, you can just start with this: > [1,10,5,-15,-2,4].sort(); [-15,-2,1,10,4,5] And yes saying "oh but it is because it has weak types" d
by rdtsc 13y ago
Sadly I agree, you can just start with this:
> [1,10,5,-15,-2,4].sort();
[-15,-2,1,10,4,5]
And yes saying "oh but it is because it has weak types" doesn't excuse it, it is broken and that's that. Naming the brokeness with a label or showing why historically it is there, doesn't fix it.
On the other hand they do have nice closure support, I do like some of that. But in large, the language makes me angry every time I have to use it.
Someone mentioned Dart, I am hopeful for that. But who knows how long till dartvm is embedded in Chrome. For now dart.js and source maps might help.
There is a bit more palatable Coffeescript. But it only puts some band-aids over JS's brokenness.
Also agree with callback hell. It is funny people extol is at the next big thing -- whoo, asynchronous calls! You can callback in your callback while you callback. No, no, no. That is a horrible way to handle large concurrency problems! (Good for quick one page examples though!). Async and friends help. But why the heck do I need to stick a callback in the middle of my logic if all I need to do is make a request to the database or another GET request?
The only sane way I know how to best handle concurrency is using a language with support of goroutines/isolates/tasks or even threads and queues. Even deferreds are just sugar on top of callbacks. We all know how Python's Twisted has taken over the world, right? Oh wait, it hasn't. Calling them Futures or Tulips or whatever doesn't fix the underlying problem.
> I can understand that some people wanted to escape the MRI infrastructure by going Node, because - you know - learning Japanese is hard.
Granted node is fast. (And according to its author scales linearly across CPU cores, sorry couldn't resist the mandatory jab at that). Ok it doesn't really scale across cores and it will block everything if you do a for loop in the middle of your logic. But it is really fast, amazingly so. And that is like crack. It gets people hooked.
EDIT: JS has weak types not just dynamic, one can have dynamic and strong types for example, like Python ( https://en.wikipedia.org/wiki/Weak_typing https://en.wikipedia.org/wiki/Weak_typing ). Thanks makslinn.
- masklinn 13y ago> And yes saying "oh but it is because it has dynamic types" doesn't excuse it It has nothing to do with dynamic types[0]. It has to do with > If compareFunction is not supplied, elements are sorted by converting them to strings and comparing strings in lexicographic[0] period end of the story. You might argue it's a shitty default, but please argue against reality, not what you (wrongly) believe it is. [0] this is the MDN translation, the exact specification is steps 14 to 18 of the SortCompare abstract operation, defined in ECMA-262 5.1 15.4.4.11 "Array.prototype.sort (comparefn)": 13. If the argument comparefn is not undefined, then a. If IsCallable(comparefn) is false, throw a TypeError exception. b. Return the result of calling the [[Call]] internal method of comparefn passing undefined as the this value and with arguments x and y. 14. Let xString be ToString(x). 15. Let yString be ToString(y). 16. If xString < yString, return -1. 17. If xString > yString, return 1. 18. Return +0. [0] your edition to "weak types" is just as wrong as the previous version I quoted was.
- rdtsc 13y agoThe correct answer is it has to do with weak types. > `If compareFunction is not supplied, elements are sorted by converting them to strings and comparing strings in lexicographic` period end of the story. As I said explaining the historical context or labeling the brokennes with something like "weak typing" doesn't excuse. It still stays broken. > You might argue it's a shitty default, but please argue against reality, not what you (wrongly) believe it is. Surprise, I know it is possible to eventually sort things in JS. I too have seen sorted tables in JS done entirely in JS. That's not the point. The point is, it is has fundamentally broken type system.
- masklinn 13y ago> The correct answer is it has to do with weak types. No, it does not. You could define the exact same default function in Java, Haskell (requiring that the sorted type be a Show instance) or Python (hell, getting the exact same behavior in Python could hardly be simpler: `ar.sort(key=str)`. There, I broke it. That is near literally how the ECMAScript spec very explicitly defines the default comparison function. The behavior of the sort function is not a consequence of weak typing). > As I said explaining the historical context or labeling the brokennes with something like "weak typing" doesn't excuse. Which you'd know is not what I'm doing, if you could be arsed to read instead of trying to substitute your imagination for objective reality. > The point is, it is has fundamentally broken type system. That might well be, but the default sort comparison function is not a demonstration of it. As opposed to the comparison table of 0 and null for instance: > 0 < null false > 0 <= null true > 0 == null false > 0 >= null true > 0 > null false
- weland 13y ago> No, it does not. You could define the exact same default function in Java, Haskell (requiring that the sorted type be a Show instance) or Python (hell, getting the exact same behavior in Python could hardly be simpler: `ar.sort(key=str)` I don't use JavaScript but to me this looks like a really broken implementation over weak typing. Of course, this isn't a trait of the typing system per se as it is a trait of the "interface", if you will, that it offers to the programmer. What OP was probably annoyed by was the typical excuse the JS community offers for this behaviour: "it's weakly-typed so you can treat the arguments as anything you want". This misses the point of having a weak typing system: instead of using it to infer the type of the arguments and get a sane default comparison function, avoiding the verbosity typical of strongly-typed languages (where you have to specify the comparison function with the correct type signature), you have to specify the comparison function for anything that isn't a string. Bummer :-(. I hope I haven't missed anything here; as I said, I'm not quite fluent in JavaScript.
- deleted 13y ago[deleted]
- robinduckett 13y ago> [1,10,5,-15,-2,4].sort(function(a,b) {return a > b}) [ -15, -2, 1, 4, 5, 10 ] > [1,10,5,-15,-2,4].sort(function(a,b) {return ""+a > ""+b}) [ -15, -2, 1, 10, 4, 5 ] Perhaps .sort was initially designed to do something else, like sorting strings, rather than integers.
- adamnemecek 13y agoI'm pretty sure that the parent comment's author is aware of this. It does not change the fact that it's broken.
- tjansen 13y agoWhy? Sorting strings is a far more common thing to do than just sorting numbers, so it makes sense to use simple string comparisons as the default algorithm. Rather than come up with something that first evaluates the types in the list and then tries to figure out what to do.
- dsl 13y agoNo, it makes sense to sort strings when you get handed strings, and sort numbers when handed numbers. Period. Default behavior is now exactly as expected by 100% of developers. Your creativity as a language designer should only come in to play when dealing with sets of mixed type.
- Yaggo 13y agoSo how should [1, "a", 2, 3] be sorted by default?
- integraton 13y agoBoth Python and Erlang would return [1,2,3,"a"], Ruby and Clojure won't allow the comparison. Either of these approaches makes more sense than JavaScript's.
- Kiro 13y agoThe sort function is fine when used as intended (for alphabetic sorting). If you want numeric sorting you use sort(function(a,b){return a-b}). [1,10,5,-15,-2,4].sort(function(a,b){return a-b}); [-15, -2, 1, 4, 5, 10]
- rdtsc 13y agoOk but if I wanted to use alphabetic sorting I would have used ["1","10","5","-15"] no? Wouldn't that make more sense.
- rmrfrmrf 13y ago[1,10,5,-15,-2,4].sort (a, b) -> a-b You're right; that was totally difficult.
- anon1385 13y agoSafari/Chrome: SyntaxError: Unexpected token '>' Firefox: SyntaxError: syntax error In a discussion about Javascript maybe it would be a good idea to give examples of Javascipt that actually run in a Javascript console?
- daleharvey 13y agoFirefox Nightly [1,10,5,-15,-2,4].sort ((a, b) => a-b).toString(); "-15,-2,1,4,5,10"
- camus2 13y agoPeople dont write JS just for Firefox Nightly.
- daleharvey 13y agoI didnt say they should (some people do), it is code which works with a JavaScript console though
- anon1385 13y agoYou have responded to my pointing out that '->' syntax doesn't work in current browsers with an example of a different bit of syntax, '=>', that works in a single development browser. I don't follow your point.
- daleharvey 13y agoSorry you asked for an example which works in a Javascript console, I gave one, if you were trying to make a snarky reply about the state of es6 support in modern browsers then I misread it.
- bartl 13y ago> [1,10,5,-15,-2,4].sort(); > [-15,-2,1,10,4,5] [1,10,5,-15,-2,4].sort(function(a,b){return a-b}) [-15, -2, 1, 4, 5, 10]
- rdtsc 13y agoSo it is broken. I already gave it a list of numbers. Why do I have to subtract them to tell it how to compare them. If I wanted to compare string I would have used ["1","10",....].