TheDamian posted some thoughts on Perl6 sorting. While this is solely a proposed syntax and semantics, I'm excited.

As is usually the case with Damian's writings, the juiciest bits (imho) are usually a bit of the way down the page. In this case, the best part is at the bottom. sort will (apparently) now handle both the Schwartzian Transform and Orcish Manuver naturally. In fact, all sorts will have the Orcish built in. (Will there be a way to turn this off? Will we care?)

The Schwartzian is taken care of by the fact that you won't be limited to just comparators anymore. You'll be able to tell sort how to find the sorting key for a given element and sort will be smart enough to know if it should sort with <=> or cmp.

In the middle, there's a really really grossly oversized sort used as an example. Let's just say I'm glad both that Perl6 will simplify things immensely and that Damian made Uri's thoughts more Perlish. As an comparison, I had started to try and build that sort in Perl5. And gave up about halfway down. (Oh - make sure you read the sorting keys/compartors from left to right, not right to left like in Perl5.)

In summary, I think that most of us will be specifying keys for our sorts instead of comparators, but I really like the idea of "dropping down" into comparator mode, if needed.

------
We are the carpenters and bricklayers of the Information Age.

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Replies are listed 'Best First'.
Re: Perl6 sorting
by kvale (Monsignor) on Mar 03, 2004 at 16:26 UTC
    I agree that the sort proposed is a powerful and useful upgrade. DWIM is much better than looking up transforms and manuevers every one wants to do something a little subtle.

    But I like Uri's key: syntax because it made it more explicit what this list of expressions is about. To my untrained eye perl5 eye, Damian's syntax looks like an anonymous array of ambiguous code blocks with funny annotations. The key: syntax feels more like natural language to me.

    -Mark

      It would be easy to write something that translated a verbose sort spec into a concise one. A "key" macro would be able to process Uri's syntax directly, for instance. But it would probably only be useful in 1% of the time--nearly all sorts are trivial. And we're adding enough new "key" words to Perl 6 as it is...
        Yes, but what would be interesting is making an approximation of this as a Perl 5 module.

        The sorting part isn't hard, IMHO: you could translate the thing into a sort-of-Schwartizan transform where an array element would be either a key, or a coderef, and sort that fairly nicely.

        Finding a good way of passing all that data to a Perl5 routine, however, would be a challenge.

        Which, I think, is why Damian chose the theme. I don't think his point was the wonderfull multitude of things his sort is going to do, it's all the wonderfull ways of communicating with subroutines that the new syntax will make possible.