TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Perl6: Unary Sort

66 pointsby patrickasover 11 years ago

7 comments

ciderpunxover 11 years ago
I really need to take a proper looks at Perl 6 -- it has some nice characteristics. Maybe that's my new years resolution.
评论 #6953915 未加载
yxhuvudover 11 years ago
Somehow I prefer the Ruby solution to add another method to do the unary variant. Compare<p><pre><code> [&quot;A&quot;,&quot;b&quot;,&quot;C&quot;].sort {|a, b| a.downcase &lt;=&gt; b.downcase } </code></pre> to<p><pre><code> [&quot;A&quot;,&quot;b&quot;,&quot;C&quot;].sort_by {|k| k.downcase } </code></pre> (or equivalently)<p><pre><code> [&quot;A&quot;,&quot;b&quot;,&quot;C&quot;].sort_by &amp;:downcase</code></pre>
评论 #6954823 未加载
adrianmsmithover 11 years ago
Great stuff, nevertheless the following is a surely a regression:<p><i>&gt; Note that in Perl 6, cmp is smart enough to compare strings with string semantics and numbers with number semantics, so producing numbers in the transformation code generally does what you want.</i><p>Perl 5 had a clear distinction between &quot;cmp&quot; (string comparison) and &quot;&lt;=&gt;&quot; (numeric comparison). Trying to work out the data type, and thus the comparison approach, from the actual data itself, is surely going to create subtle bugs, that don&#x27;t appear in testing, but do appear with live data.
评论 #6954155 未加载
评论 #6954537 未加载
rmcover 11 years ago
Python has this with the &quot;key&quot; argument to &quot;sort&quot;.<p><pre><code> list.sort(key=lambda x: x.lower())</code></pre>
评论 #6955400 未加载
评论 #6954429 未加载
orblivionover 11 years ago
One thing this article does not cover is that, even if a comparison function doesn&#x27;t do anything slow, there is still the fact that it still has to do a Perl function call O(n log n) times. At least in the equivalent with Python, I think it is advised to use the key function rather than comparison funtion for this reason, for speed. Though I guess a key function needs to take more space if it is to memoize.
评论 #6954655 未加载
Grue3over 11 years ago
So, it&#x27;s a feature that has been available in every high-order language since forever, except with more line noise. Typical Perl!<p><a href="http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/fun_sortcm_stable-sort.html" rel="nofollow">http:&#x2F;&#x2F;www.ai.mit.edu&#x2F;projects&#x2F;iiip&#x2F;doc&#x2F;CommonLISP&#x2F;HyperSpec...</a>
tarpdenover 11 years ago
While it appears that Perl 6 has many impressive features, I&#x27;m much more interested in practical matters such as: module versioning, installation, &amp; removal; the state of MoarVM; and the state of the tutorial book.
评论 #6958771 未加载