January 09, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5076


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
         AssignedTo|nobody@puremagic.com        |andrei@metalanguage.com


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 24, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5076



--- Comment #10 from bearophile_hugs@eml.cc 2011-01-24 03:04:20 PST ---
In Python sort() is in-place. To help programmers remember this, sort() returns
None (like void in D):

>>> a = [10, 30, 5]
>>> sorted(a)
[5, 10, 30]
>>> a
[10, 30, 5]
>>> a.sort()
>>> a
[5, 10, 30]

But in DMD 2.051 std.algorithm.sort() returns the sequence sorted in-place.
Once a sorted/schwartzSorted are present, I suggest to let std.algorithm.sort()
return void, as in Python.

sorted/schwartzSorted may be tagged with @nodiscard from bug 5464 to further help programmers remember their semantics.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 19, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5076



--- Comment #11 from bearophile_hugs@eml.cc 2011-05-18 18:21:42 PDT ---
See also issue 6035

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 29, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5076



--- Comment #12 from bearophile_hugs@eml.cc 2011-10-28 17:33:16 PDT ---
An use case for sorted(). I have to create a function foo() with a int[]
argument. Unless foo() is performance-critical the usual API requirements ask
for its arguments to be constant (in), to make the program less bug-prone.
Inside foo() I need to sort the a copy of items, and then I don't need to
modify this array, so I'd like this array copy too to be const. This is an
implementation that currently works:


import std.algorithm, std.exception;
void foo(in int[] unsortedData) {
    int[] tmpData_ = unsortedData.dup;
    tmpData_.sort();
    const(int[]) data = assumeUnique(tmpData_);
    // Use array 'data' here.
}
void main() {}


assumeUnique is not safe, and the tmpData_ name is present in the scope still (despite assumeUnique has turned its length to zero, this improves the situation a little).

With a pure sorted(), the code becomes more clean and safe:


import std.algorithm;
void foo(in int[] unsortedData) pure {
    const(int[]) data = sorted(unsortedData);
    // Use array 'data' here.
}
void main() {}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 07, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=5076


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com


--- Comment #13 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-02-07 12:20:02 PST ---
I run into this issue all the time, in particular when doing script-based programming. E.g.:

string[string] classes;
foreach (name; classes.keys.sorted) { } // ng

Having to do this is a chore:
auto keys = classes.keys;
sort(keys);
foreach (name; keys) { }

This works ok for my purposes (yada yada about constraints):
T sorted(T)(T t)
{
    T result = t;
    sort(result);
    return result;
}

Why do we have replace and replaceInPlace, whereas we have sort which sorts in place implicitly? "findSkip" is another function that I sometimes use but I hate how it hides the fact that it modifies its arguments. "find" returns a range, but "findSkip" returns a bool and *modifies* your argument. It's not at all obvious from the call site.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
1 2
Next ›   Last »