There's more than one way to do things | |
PerlMonks |
Re: Noodling with natural sorting in perl6by thundergnat (Deacon) |
on Aug 25, 2010 at 18:17 UTC ( [id://857264]=note: print w/replies, xml ) | Need Help?? |
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.
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
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. :-)
Update: Actually, since perl6 has schwartzian transform semantics built into .sort, this could be very concisely rewritten as:
or, my favorite:
used as
That's just beautiful.
In Section
Meditations
|
|