[Issue 4909] New: Two suggestions for std.algorithm.schwartzSort()

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Sep 21 12:59:16 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4909

           Summary: Two suggestions for std.algorithm.schwartzSort()
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2010-09-21 12:58:38 PDT ---
Both my experience with Python and some practice with D v2 shows me that in
script-like programs schwartzSort() is useful very often. Python 3 has even
removed the "cmp" argument for its built-in sort, so it always performs a
Schwartz sorting using the "key" argument, because it's simpler to use.

This an usage example of schwartzSort usage, to sort an array of arrays
according to the second item in the sub-arrays:


import std.algorithm;
void main() {
    auto arr = [[5,9],[5,3],[4,12],[7,8],[12,2],[5,8]];
    schwartzSort!((e){ return e[1]; })(arr);
    assert(arr == [[12,2],[5,3],[7,8],[5,8],[5,9],[4,12]]);
}


To improve the usage of schwartzSort a support for transform function expressed
as string may be added.

For non-English speaking programmers the spelling of schwartzSort is not easy,
and a so commonly used function may enjoy a shorter name any way, so a
"keySort" name may be better ("key" refers to the key argument function of the
Python sort, that's named "transform" in Phobos):


import std.algorithm;
void main() {
    auto arr = [[5,9],[5,3],[4,12],[7,8],[12,2],[5,8]];
    keySort!q{a[1]}(arr);
    assert(arr == [[12,2],[5,3],[7,8],[5,8],[5,9],[4,12]]);
}


To help the understanding of this "keySort" name, in the signature of keySort
the "transform" argument may be renamed "key".

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list