»»» This post is about the immature Perl 6, not the rock solid Perl 5 ««« |
I often share small things about P6. This is perhaps the smallest yet...
So I just encountered this series of recent blog posts by someone exploring P6. Anyhoo, this meditation isn't about what he's written but the P6 feature brought to my attention by a comment added to his 5th post which shows something like this:
say sort +*, @array;
Apparently this is akin to a schwartzian transform (which a 4 year old PM post by Moritz suggests is implemented more efficiently than an actual schwartzian transform).
Update At least two monks failed to see a strong (or any) connection between say sort +*, @array and the ST. In summary the * is a single arg "key extractor" closure, and the P6 sort builtin automatically turns @sorted = sort +*, @array in to something like the P5 code:
@sorted = map $_->[0], sort { $a->[1] <=> $b->[1] } map [ $_, +$_ ], @array;
/Update
Perhaps everyone else already knew this, but I didn't and, weeell... I think it's amazingly short and it came directly on the heels of another tiny triumph of P6 brevity.
Of course, the main point of the transform is speed, and as the author of the quoted blog mentions in one of his posts, P6 is still slooow (even if it has gotten something like an order of magnitude faster this year for many tasks). But a secondary point of the transform is that it's a nice idiom and I think this is another example of the wide array of nice P6 idioms.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: sort +*, @array
by BrowserUk (Patriarch) on Dec 09, 2013 at 02:36 UTC | |
by jdporter (Paladin) on Dec 09, 2013 at 04:05 UTC | |
by raiph (Deacon) on Dec 09, 2013 at 12:16 UTC | |
by jdporter (Paladin) on Dec 09, 2013 at 18:36 UTC | |
by raiph (Deacon) on Dec 10, 2013 at 07:24 UTC | |
by Anonymous Monk on Dec 10, 2013 at 21:31 UTC | |
by BrowserUk (Patriarch) on Dec 09, 2013 at 15:54 UTC | |
by raiph (Deacon) on Dec 10, 2013 at 07:10 UTC | |
Re: sort +*, @array
by hdb (Monsignor) on Dec 09, 2013 at 10:09 UTC | |
by raiph (Deacon) on Dec 09, 2013 at 12:32 UTC | |
by hdb (Monsignor) on Dec 09, 2013 at 12:39 UTC | |
by raiph (Deacon) on Dec 09, 2013 at 13:53 UTC | |
by hdb (Monsignor) on Dec 09, 2013 at 14:26 UTC | |
by moritz (Cardinal) on Dec 11, 2013 at 12:27 UTC | |
Re: sort +*, @array
by sundialsvc4 (Abbot) on Dec 10, 2013 at 16:21 UTC | |
by raiph (Deacon) on Dec 10, 2013 at 23:03 UTC | |
by hdb (Monsignor) on Dec 11, 2013 at 07:11 UTC | |
by BrowserUk (Patriarch) on Dec 11, 2013 at 08:09 UTC | |
by raiph (Deacon) on Dec 11, 2013 at 19:57 UTC | |
| |
|