I'm not saying that the design of the P6 builtin is ST, just that it is functionally equivalent, and even more concise.

And it's not just about the "decorate" step, which your "key extractor" closure is roughly equivalent of.

One critical point: while a "key extractor" closure is indeed roughly equivalent to the decorate step in terms of the role it's playing, it's multiple key extractor closures that is basically fully equivalent to the before map.

Because the ST has an explicit sort block (normally, unless it's the GRT variant :-)), it allows to execute arbitrarily complex Perl code in the comparison. Where does one do that in this Perl 6 "equivalent"?

You just specify a comparator closure (or multiple, if you want).

(P6 can tell the difference due to a closure's number of args. If it has one, it's a key extractor closure, if it has two it's a comparator closure.)

For lots more examples which are still correct modulo a few minor syntax shifts, see "The Sort Problem: a definitive ruling" from 2004.

Which got me to focus on the Pair variant which maybe helps clarify the connection for you:

sort { #`( key extractor closure) } => { #`( comparator closure) }, { #`( key extractor closure) } => { #`( comparator closure) }, ... @array


In reply to Re^4: sort +*, @array by raiph
in thread sort +*, @array by raiph

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.