Live data from Hacker News

No safe efficient ways to do three-way string comparisons in Go

go101.org

101–110 of 154 posts

Re: No safe efficient ways to do three-way string comparisons in Go

#101
Maybe I'm not understanding the articles premise exactly, but if you're using three-way comparisons frequently, why would you not just implement the `diff3` ?

It's not that complicated and well documented, infact I'm sure someone has already done it in go.

http://www.cis.upenn.edu/~bcpierce/papers/diff3-short.pdf

Re: No safe efficient ways to do three-way string comparisons in Go

#102
post #98

Earlier quoted context omitted.

That is what happens when they refuse to adopt a deprecation process, even Java, .NET, C and C++ with their high regard for backwards compatibility do have such processes, and have removed features from standard library and languages.

Go marks features as deprecated, though, both in source code and in the docs. For example, strings.Title() has been deprecated and instead the case package should be used. The backwards guarantee merely means that the feature won't be removed from the library in Go 1.x, but when you write new code you won't use it and the preferred way of doing the same is mentioned in the docs and version notes. I think it's good no…

Except Go certainly is never getting another major version, I am waiting for Go 1.10000.0, given how decisions are made on the ecosystem.

Re: No safe efficient ways to do three-way string comparisons in Go

#103

Maybe I'm not understanding the articles premise exactly, but if you're using three-way comparisons frequently, why would you not just implement the `diff3` ? It's not that complicated and well documented, infact I'm sure someone has already done it in go. http://www.cis.upenn.edu/~bcpierce/papers/diff3-short.pdf

It's not three-way as in comparing three strings; here they're comparing two strings, with a "three-way" result: ab

Re: No safe efficient ways to do three-way string comparisons in Go

#104
post #82

Earlier quoted context omitted.

This was a joke right?

No I’m being sincere. Clarity is more important than performance. This is a very thoughtful decision, it relieves devs of the idiotic pressure to micro optimize.

It's amusing to mention clarity, given the topic. Because the cargo-cult of this kind of function having return type 'int' is lack of clear thinking manifest. Int has 4-billion distinct values and all they want is three.

Here's clarity:

datatype order https://smlfamily.github.io/Basis/general.html#SIG:GENERAL.o...

function compare https://smlfamily.github.io/Basis/string.html#SIG:STRING.com...

    case String.compare(a, b) of
    | LESS    => ...
    | EQUAL   => ...
    | GREATER => ...

Re: No safe efficient ways to do three-way string comparisons in Go

#105
post #38

Earlier quoted context omitted.

No one will follow along. If you want to implement strings.Compare better, you can write a package and publish it for other folks to import. The function is not particularly special.

No one will follow along. "The only sure path to failure is to not even try." Or are you both implying the power of and supporting the massive monopoly that Google already has?

> Or are you both implying the power of and supporting the massive monopoly that Google already has?

This is not a reasonable assessment.

Golang is massive; why would anyone switch to not-Golang which isn't backed by a large organisation and just has one string comparison optimisation?

And why would anyone trust a forking entity that forked a runtime to make a change that could be packaged as a library, as the comment you're replying to already suggests?

Re: No safe efficient ways to do three-way string comparisons in Go

#106

Earlier quoted context omitted.

Then you can use bytewise comparison instead of alphabetic comparison.

Assuming you normalized the strings before.

Why would that matter, if you only want some well-defined order?

Re: No safe efficient ways to do three-way string comparisons in Go

#108
post #80

Earlier quoted context omitted.

The comment is confusing, but the idea seems to be that instead of calling this function, you should inline the code - that is, just write the comparisons yourself. You don't need a function call. I guess this is for stylistic reasons, but I don't know why anyone would feel strongly about doing it one way or the other.

I don’t think that’s true. I believe they want you to explicitly define how you want you compare strings. Comparing Unicode strings isn’t as straightforward as comparing individual bytes or code points. For example, there are multiple Unicode strings that will yield the character “ï”. If you use a naïve comparison function, you can’t be sure that it will behave as expected when it encounters the word “naïve”.

You’re looking for Collator.CompareString

https://pkg.go.dev/golang.org/x/text/collate#Collator.Compar...

Re: No safe efficient ways to do three-way string comparisons in Go

#109
post #29

Earlier quoted context omitted.

For some applications (eg sticking stuff in an ordered data structure), you just need any consistent ordering, but don't care too much about exactly which one.

I've been developing my own version string parser for a couple weeks now, in golang. It's ridiculous to what lengths you have to go to understand which part of a string comes earlier or later. Simple example: semantic versioning allows "1.2.3alpha" and also 1.2.3-beta", but which one comes first now... - Is 1.2.3 > 1.2.3omega? - Is 1.2.3 > 1.2.3beta? - Is 1.2.3gamma > 1.2.3? In the Linux world it gets even funnier ca…

I'd use capture regex to get the first three numerals and capture the remaining string. If the remaining string exists, you can easily ignore the expected leading dash and your malformed semver suffixes will work and those can be trivially compared/sorted.

What about Go makes this different? That's how I'd solve this is any language

Re: No safe efficient ways to do three-way string comparisons in Go

#110

Earlier quoted context omitted.

> so the documentation is trying to talk them out of... What documentation though? There's an internal comment but it's not documentation and it's not really arguing against the use of this type of comparison (just against use of the implemented function). If the documentation for this function went on along the lines of "we discourage the use of 3 way compares as we consider it an antipattern, do X instead, there's…

> If the documentation for this function went on along the lines of "we discourage the use of 3 way compares as we consider it an antipattern, do X instead, there's literally no reason to ever use 3 way compares, we know it, here's why..." maybe, but it doesn't. But it does. It literally says "It is usually clearer and always faster to use the built-in string comparison" and it always said that. https://pkg.go.dev/st…

It doesn't go into why, it's just a statement.
Post reply on HN