And now, even more perl sixy...

natural_sort can be rewritten to do no copying and use no intermediate variables. It's kind of obfu but a lot more memory efficient. The » is the hyper operator. It is sort of like map. It means apply that method to each element in the supplied list.

sub natural_sort { return @_ if @_ < 2; my $index = 0; return @_[@_».lc».subst(/(\d+)/, -> $/{ sprintf( "%s%c%s", 0, $0.c +hars, $0) }, :g).map( *~' '~$index++ ).sort.map({@(split(' ',$^_))[*- +1]})]; }

natural_cmp can also use the hyper operator and is now using leg instead of cmp. leg is the string specific comparison operator. (l)ess-(e)qual-(g)reater

sub natural_cmp ($a, $b) { return [leg] ($a, $b)».lc».subst(/(\d+)/, -> $/{ sprintf( "%s%c%s", + 0, $0.chars, $0) }, :g); }

A cached version could look something like below. Note: the cached version actually runs slower than the uncached version right now because of a bug in Rakudo * 2010.07. It evaluates both sides of the //= even if the lhs is defined. :-/ That should be fixed in the near future though. :-)

my %cache; sub cached_natural_cmp ($a, $b) { return [leg] ($a, $b)».lc.map( %cache{ * } //= *.subst(/(\d+)/, -> + $/{ sprintf( "%s%c%s", 0, $0.chars, $0) }, :g)); }

Update: Actually, since perl6 has schwartzian transform semantics built into .sort, this could be very concisely rewritten as:

@array.sort(*.lc.subst(/(\d+)/, -> $/{ sprintf( "%s%c%s", 0, $0.chars +, $0) }, :g));

or, my favorite:

@array.sort: natural(*); sub natural ($) { *.lc.subst(/(\d+)/, -> $/{ sprintf( "%s%c%s", 0, $0. +chars, $0) }, :g) }

used as

.say for %words.keys.sort: natural(*); or %words.keys.sort( natural(*) )».say;

That's just beautiful.


In reply to Re: Noodling with natural sorting in perl6 by thundergnat
in thread Noodling with natural sorting in perl6 by thundergnat

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.