https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112017-08-30T06:37:28ZRuby Issue Tracking SystemRuby master - Misc #13840: Collection methods - stabilityhttps://bugs.ruby-lang.org/issues/13840?journal_id=663322017-08-30T06:37:28Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul></ul><p>MSP-Greg (Greg L) wrote:</p>
<blockquote>
<p>Regardless, in simplifying the code, I was left with one question regarding all of the sort/filter group methods in ruby.</p>
<p>Which ones are considered stable, or, is original order maintained where applicable?</p>
</blockquote>
<p>As described below, this seems to lump together two different families of methods.</p>
<blockquote>
<p>Obviously, this pertains to <code>sort</code> and <code>sort_by</code>, but also has meaning in <code>group_by</code>, <code>select</code>, <code>reject</code>, and similar methods.</p>
<p>I'm not proposing one or another, although I'd prefer stable. Docs for all these methods should note this,</p>
</blockquote>
<p>The purpose of sorting is to change the order of the elements (except for trivial input), and <em>stable</em> refers only to those elements that compare equal under the sort order. For sorting, efficient implementations (quicksort being the most famous one) are not stable, and therefore, "stable or not stable" is a standard question for a sorting algorithm. Array#sort, Array#sort!, Array#sort_by!, Enumerable#sort, and Enumerable#sort_by all are not stable, and all clearly say so in the documentation. I would expect any other sort-like methods in Ruby to also not guarantee stability, and to say so explicitly. If you find one that doesn't, please tell us explicitly.</p>
<p>In contrast, none of the partitioning methods in any way 'intends' to change the order of the elements, and the principle of least astonishment implies that order doesn't change. (Non-parallel) implementations of partitioning methods are straightforward and automatically stable. I have never heard of a non-stable filter/select method, and using the term 'stable' for these kinds of methods isn't customary because it's default/obvious/automatic. That may be the reason why the docs don't say anything for these methods. Many readers would have to think twice what was meant by 'stable' in such a method's context, and might wonder why this is mentioned at all. I would therefore expect all the partitioning methods (group_by, select, reject, partition and friends) to be 'stable' in your sense, without the need for explicit documentation.</p>
<blockquote>
<p>and if stability is guaranteed, tests should verify it. Happy to help.</p>
</blockquote>
<p>Tests can't verify. If a method that's intended to be stable got unstable as a result of a bug, it may be good to have a regression test. Also, tests may be a good idea for methods where actual effort is needed to make them stable (such as a stable sort, but Ruby doesn't have one).</p>
<blockquote>
<p>As an aside, Gem::Resolver#search_for exists in both ruby and rubygems, but is different. Also, a test for it exists in rubygems, but not in ruby. Seems odd.</p>
</blockquote>
<p>Separate issue, please (if necessary).</p> Ruby master - Misc #13840: Collection methods - stabilityhttps://bugs.ruby-lang.org/issues/13840?journal_id=673782017-10-20T02:03:14Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li></ul><p>(Rejecting for now but please don't hesitate to open a new one for the Gem::Resolver issue if necessary.)</p>
<p>As Martin already pointed out our documents clearly say that sorting methods are not stable.</p>