2 ms·
<=> has some of the semantics of Fortran's "arithmetic" IF statement. This can be useful, as the Fine Article shows, in sorts and searches, I've rolled it by h
by samatman 5y ago
<=> has some of the semantics of Fortran's "arithmetic" IF statement.
This can be useful, as the Fine Article shows, in sorts and searches, I've rolled it by hand a few times for tests like "is this offset in the line we're examining".
- omoikane 5y ago<=> and cmp also have the benefit that fewer comparisons are done. It might not be that big a deal for Perl scalars, but for C++, an overloaded operator< that does something like this: return left.a != right.a ? left.a < right.a : left.b < right.b; That might result in exponential number of compare operations if those nested comparisons are also overloaded, whereas with <=> (as of C++20), the number of compare operations can be made to be linear: return left.a <=> right.a < 0 || left.b <=> right.b < 0;
- omoikane 5y agoSorry, that should have been: return !(left.a <=> right.a >= 0 && left.b <=> right.b >= 0);