News for hackers. Stuff that matters.
Re:Clear is better than clever [pdf] by __s on Monday May 06, 2019 @07:46AM (#19838482)
Meanwhile I'll write (a>b)-(a<b), which is what Python suggests after having removed cmp
Re:Clear is better than clever [pdf] by sametmax on Monday May 06, 2019 @08:47AM (#19838885)
No, what Python suggests is just returning the value you sort on, or a tuple of the values to sort on.

cmp() is deprecated because there is seldom reason to use it anymore.

E.G:

Sorting words according to their number of letter is just:

    sorted(words, key=len)
You don't return 1, -1 or 0.

If you want to rank participants of a game in a dictionary of scores using names as keys and points as values, you would to:

    sorted(scores.items(), key=lambda score: score[1])
Again you don't return 1, -1 or 0, just the value that is significant. It makes it very, very simple to reason about.
Re:Clear is better than clever [pdf] by __s on Monday May 06, 2019 @07:13PM (#19844884)
Official docs: https://docs.python.org/3.0/whatsnew/3.0.html#ordering-compa...

> If you really need the cmp() functionality, you could use the expression (a > b) - (a < b) as the equivalent for cmp(a, b)

Re:Clear is better than clever [pdf] by sametmax on Tuesday May 07, 2019 @05:41AM (#19847863)
αν
Re:Clear is better than clever [pdf] by alexgmcm on Monday May 06, 2019 @08:14AM (#19838661)
I mainly use Python but that would still be unclear to me.

I think the ternary operator is the sweet spot between too verbose and too confusing.

Re:Clear is better than clever [pdf] by userbinator on Monday May 06, 2019 @08:11AM (#19838636)
That works nicely too, for those languages with "1-0" booleans. For languages without them (Java, C# come to mind), the ternary is the next-best way.