November 10, 2014 Re: sortOn: sorts range of aggregates by member name(s) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | Andrei Alexandrescu:
> Sadly things don't work that way - e.g. Phobos1 took a bunch of string functions from Ruby and Python, to no notable effect.
If you port Python code to D (and this is something I do almost every day) the Phobos string functions help making the porting simpler.
Bye,
bearophile
| |||
November 10, 2014 Re: sortOn: sorts range of aggregates by member name(s) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 11/10/14 12:56 AM, bearophile wrote:
> Andrei Alexandrescu:
>
>> Sadly things don't work that way - e.g. Phobos1 took a bunch of string
>> functions from Ruby and Python, to no notable effect.
>
> If you port Python code to D (and this is something I do almost every
> day) the Phobos string functions help making the porting simpler.
>
> Bye,
> bearophile
Good to know, thanks! -- Andrei
| |||
November 10, 2014 Re: sortOn: sorts range of aggregates by member name(s) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Monday, 10 November 2014 at 03:38:49 UTC, Andrei Alexandrescu wrote: > That's a better approach, thanks. A few compelling examples would help. > > > Andrei One more thing. How should reverse sorting be handled in elegant way? Like this https://github.com/nordlow/justd/blob/master/sort_ex.d#L57 or via some other clever solution I haven't thought about? | |||
November 10, 2014 Re: sortOn: sorts range of aggregates by member name(s) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On 11/10/14 11:58 AM, "Nordlöw" wrote:
> On Monday, 10 November 2014 at 03:38:49 UTC, Andrei Alexandrescu wrote:
>> That's a better approach, thanks. A few compelling examples would help.
>>
>>
>> Andrei
>
> One more thing. How should reverse sorting be handled in elegant way?
> Like this
>
> https://github.com/nordlow/justd/blob/master/sort_ex.d#L57
>
> or via some other clever solution I haven't thought about?
For most numeric cases having the caller negate the key should suffice. For string types you'd need a separate function or fall back to classic sort. This, too, doesn't bode well for the power/weight ratio of sortBy. -- Andrei
| |||
November 10, 2014 Re: sortOn: sorts range of aggregates by member name(s) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Monday, 10 November 2014 at 19:58:32 UTC, Nordlöw wrote:
> On Monday, 10 November 2014 at 03:38:49 UTC, Andrei Alexandrescu wrote:
>> That's a better approach, thanks. A few compelling examples would help.
>>
>>
>> Andrei
>
> One more thing. How should reverse sorting be handled in elegant way? Like this
>
> https://github.com/nordlow/justd/blob/master/sort_ex.d#L57
>
> or via some other clever solution I haven't thought about?
By chaining it with std.range.retro? But this will of course be lazy, and will lose the information that the range is sorted.
Maybe there doesn't need to be a special facility for that, if it's allowed to specify arbitrary expressions?
container.sortBy!(a => a.intMember) // ascending
container.sortBy!(a => -a.intMember) // descending
Or shorter (the expression will be mixed in):
container.sortBy!" intMember"; // ascending
container.sortBy!"-intMember"; // descending
| |||
November 10, 2014 Re: sortOn: sorts range of aggregates by member name(s) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Monday, 10 November 2014 at 03:38:49 UTC, Andrei Alexandrescu wrote:
> On 11/9/14 4:49 AM, "Nordlöw" wrote:
>> On Sunday, 9 November 2014 at 08:12:37 UTC, Andrei Alexandrescu wrote:
>>> sortBy!(expr) is equivalent with sort!(expr_a < expr_b) so there's no
>>> additional power to it. However, it does make some sorting criteria
>>> easier to write. A while ago I took a related diff pretty far but in
>>> the end decided to discard it because it didn't add sufficient value.
>>
>> What do you mean by sufficient value?
>
> It seemed to me just another way of doing the same things.
>
>> Ruby has sort_by. I believe D would when possible should be a superset
>> of the union of powers available in other languages to maximize the ease
>> of porting code *to* D and the pleasure for programmers to switch to D.
>
> Sadly things don't work that way - e.g. Phobos1 took a bunch of string functions from Ruby and Python, to no notable effect.
>
>> I'll try to make this elegant and useful and use it myself for a while.
>> If it works for me I'll do a PR still. Ok?
>
> That's a better approach, thanks. A few compelling examples would help.
A positive point about it would be that it's more intuitive to read, especially with long names or complex expressions.
// sort vectors by length
container.sortBy!"x*x + y*y";
// vs.
container.sort!((a,b) => a.x*a.x + a.y*a.y < b.x*b.x + b.y*b.y);
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply