Live data from Hacker News

How to write X in both Python 3 and JavaScript

sayazamurai.github.io

61–70 of 84 posts

Re: How to write X in both Python 3 and JavaScript

#61
post #58

Earlier quoted context omitted.

It depends on whether or not you pass it a basic comparator function, eg: someArray.sort((a,b) => a - b); But yeah the example would have needed this for the less complex example as well to be accurate.

Shouldn't that be `a < b`?

That will give you a sequence of decreasing numbers. `a-b` will work just as `a > b`.

If `a-b` returns a negative number, the result will be treated as -1. If the numbers are the same, then you end up with 0, and so on.

I don't know what's faster in that case.

Re: How to write X in both Python 3 and JavaScript

#62
post #58

Earlier quoted context omitted.

It depends on whether or not you pass it a basic comparator function, eg: someArray.sort((a,b) => a - b); But yeah the example would have needed this for the less complex example as well to be accurate.

Shouldn't that be `a < b`?

No, the compare function needs to be able to indicate when two values are equal, not just when one is smaller/larger than the other. Otherwise your sort function will return inconsistent results for compare(a, b) and compare(b, a) when both values are equal.

Re: How to write X in both Python 3 and JavaScript

#64
post #58

Earlier quoted context omitted.

Shouldn't that be `a < b`?

That will give you a sequence of decreasing numbers. `a-b` will work just as `a > b`. If `a-b` returns a negative number, the result will be treated as -1. If the numbers are the same, then you end up with 0, and so on. I don't know what's faster in that case.

a > b is not a consistent comparison function. The behavior of sort() with such a function is implementation-defined.

Re: How to write X in both Python 3 and JavaScript

#67
post #13

As someone who is often switching languages, I find these sorts of cheatsheets useful. I know all the syntax in them but I'll be damned if I can ever remember which language uses which until I've settled back in to things.

You might find Rosetta Code to be useful - it's a wiki site with hundreds of examples, all with solutions in dozens of languages for comparison.

ikewise, I felt a little let down. On the flipside, it's an excellent opportunity to bring Rosetta Code to people's attention - a wiki with hundreds of example problems, each solved in many languages for comparison!

https://www.rosettacode.org/wiki/Rosetta_Code

I find it invaluable in that sort of mid-learning level of a new language, where I have the syntax sorted, but I need lots of programs small enough to hold in my head, but also large enough to show off all the features, to learn and read. Here's Dijkstra, for example: https://www.rosettacode.org/wiki/Dijkstra%27s_algorithm

Re: How to write X in both Python 3 and JavaScript

#68
When I publish code about algorithms and data structures to my web site, I usually offer multiple language versions. Why? Because understanding the computer science theory and math proofs is a big effort. Writing and debugging my first implementation in any language is a big effort. But porting that code to a bunch of languages is comparatively easy and is almost a thoughtless mechanical process.

Re: How to write X in both Python 3 and JavaScript

#69

Sort example doesn't take into account Javascript does lexicographical sorting? javascript: someList = [ 40, 2, 1, 3, 7, 99] someList.sort() Array(6) [ 1, 2, 3, 40, 7, 99 ] python: someList = [ 40, 2, 1, 3, 7, 99] sorted(someList) [1, 2, 3, 7, 40, 99]

Your comment led me to discover https://www.reddit.com/r/softwaregore and reaffirm myself that I should stay away from javascript for as long as I can

Re: How to write X in both Python 3 and JavaScript

#70
post #64

Earlier quoted context omitted.

That will give you a sequence of decreasing numbers. `a-b` will work just as `a > b`. If `a-b` returns a negative number, the result will be treated as -1. If the numbers are the same, then you end up with 0, and so on. I don't know what's faster in that case.

a > b is not a consistent comparison function. The behavior of sort() with such a function is implementation-defined.

Agreed. I wouldn't use it the way shown here. I would make sure to return a hard negative, zero, or one in any case.

I was surprised when I read into how many different ways `Array.prototype.sort()` is implemented across environments.

Post reply on HN