Jump to page: 1 2 3
Thread overview
.sort vs sort(): std.algorithm not up to the task?
Jun 08, 2017
Andrew Edwards
Jun 08, 2017
Mike B Johnson
Jun 08, 2017
Andrew Edwards
Jun 08, 2017
Andrew Edwards
Jun 08, 2017
Era Scarecrow
Jun 08, 2017
Stanislav Blinov
Jun 08, 2017
Andrew Edwards
Jun 08, 2017
Jonathan M Davis
Jun 08, 2017
Stanislav Blinov
Jun 08, 2017
Jonathan M Davis
Jun 08, 2017
Andrew Edwards
Jun 08, 2017
Jonathan M Davis
Jun 08, 2017
Andrew Edwards
Jun 08, 2017
Stanislav Blinov
Jun 08, 2017
Andrew Edwards
Jun 08, 2017
Jonathan M Davis
Jun 08, 2017
Andrew Edwards
Jun 08, 2017
Russel Winder
Jun 08, 2017
Russel Winder
Jun 08, 2017
Seb
Jun 08, 2017
9il
Jun 08, 2017
Timon Gehr
Jun 08, 2017
Timon Gehr
June 08, 2017
Ranges may be finite or infinite but, while the destination may be unreachable, we can definitely tell how far we've traveled. So why doesn't this work?

import std.traits;
import std.range;

void main()
{
    string[string] aa;

    // what others have referred to as
    // standard sort works but is deprecated
    //auto keys = aa.keys.sort;

    // Error: cannot infer argument types, expected 1 argument, not 2
    import std.algorithm: sort;
    auto keys = aa.keys.sort();

    // this works but why should I have to?
    //import std.array: array;
    //auto keys = aa.keys.sort().array;

    foreach (i, v; keys){}
}

If I hand you a chihuahua for grooming, why am I getting back a pit bull? I simply want a groomed chihuahua. Why do I need to consult a wizard to get back a groomed chihuahua?
June 08, 2017
On Thursday, 8 June 2017 at 01:57:47 UTC, Andrew Edwards wrote:
> If I hand you a chihuahua for grooming, why am I getting back a pit bull? I simply want a groomed chihuahua. Why do I need to consult a wizard to get back a groomed chihuahua?

Because is like a poodle and unless you get your hair cut in a special way you won't be considered for the job! The wizard only exists to balance the symmetric arrangement of the interplanetary forces, don't fear him, he does his job in a very predictable way.

June 08, 2017
On Thursday, 8 June 2017 at 02:07:07 UTC, Mike B Johnson wrote:
> On Thursday, 8 June 2017 at 01:57:47 UTC, Andrew Edwards wrote:
>> If I hand you a chihuahua for grooming, why am I getting back a pit bull? I simply want a groomed chihuahua. Why do I need to consult a wizard to get back a groomed chihuahua?
>
> Because is like a poodle and unless you get your hair cut in a special way you won't be considered for the job! The wizard only exists to balance the symmetric arrangement of the interplanetary forces, don't fear him, he does his job in a very predictable way.

Pretty funny. But seriously, this is something that just work. There is now to layers of indirection to achieve what I used to do quite naturally in the language.
June 08, 2017
On Thursday, 8 June 2017 at 02:19:15 UTC, Andrew Edwards wrote:
> Pretty funny. But seriously, this is something that just work. There is now to layers of indirection to achieve what I used to do quite naturally in the language.

*should just work
June 08, 2017
On Thursday, 8 June 2017 at 01:57:47 UTC, Andrew Edwards wrote:
> Ranges may be finite or infinite but, while the destination may be unreachable, we can definitely tell how far we've traveled. So why doesn't this work?
>
> import std.traits;
> import std.range;
>
> void main()
> {
>     string[string] aa;
>
>     // what others have referred to as
>     // standard sort works but is deprecated
>     //auto keys = aa.keys.sort;
>
>     // Error: cannot infer argument types, expected 1 argument, not 2
>     import std.algorithm: sort;
>     auto keys = aa.keys.sort();
>
>     // this works but why should I have to?
>     //import std.array: array;
>     //auto keys = aa.keys.sort().array;
>
>     foreach (i, v; keys){}
> }
>
> If I hand you a chihuahua for grooming, why am I getting back a pit bull? I simply want a groomed chihuahua. Why do I need to consult a wizard to get back a groomed chihuahua?

aa.keys.sort() should just work as is: aa.keys returns a string[], and that's a random access range that can be sorted. What exactly is the error?
June 08, 2017
On Thursday, 8 June 2017 at 02:21:03 UTC, Stanislav Blinov wrote:
>
> aa.keys.sort() should just work as is: aa.keys returns a string[], and that's a random access range that can be sorted. What exactly is the error?

It does not ... I provided the code and related error message. See the line right above "import std.algorith: sort;".
June 07, 2017
On Thursday, June 08, 2017 01:57:47 Andrew Edwards via Digitalmars-d-learn wrote:
> Ranges may be finite or infinite but, while the destination may be unreachable, we can definitely tell how far we've traveled. So why doesn't this work?
>
> import std.traits;
> import std.range;
>
> void main()
> {
>      string[string] aa;
>
>      // what others have referred to as
>      // standard sort works but is deprecated
>      //auto keys = aa.keys.sort;
>
>      // Error: cannot infer argument types, expected 1 argument,
> not 2
>      import std.algorithm: sort;
>      auto keys = aa.keys.sort();
>
>      // this works but why should I have to?
>      //import std.array: array;
>      //auto keys = aa.keys.sort().array;
>
>      foreach (i, v; keys){}
> }

Ranges do not support iterating with an index. The workaround if you want to have an index with ranges and foreach, then you should use lockstep:

http://dlang.org/phobos/std_range.html#lockstep

e.g.

foreach(i, v; lockstep(iota!size_t(0), s))
{}

or

foreach(i, v; lockstep(iota(0), s))
{}

if you don't mind i being an int instead of a size_t.

Now, with your example, there are two other solutions that are even easier. sort() sorts the range that it's given in place but returns a wrapper range so that other algorithms can recognize the range as sorted. So, if you did

auto keys = aa.keys;
sort(keys);

foreach(i; v)
{}

then it will work just fine, because you're still dealing with an array. Alternatively, you could use SortedRange's release function to get the array out of the SortedRange (though note if you do that, you shouldn't then use the SortedRange anymore, since it does a move call to actual move the wrapped range out of the SortedRange). e.g.

auto keys = aa.keys.sort().release();

foreach(i; v)
{}

and that would allow you to still do it in one line.

- Jonathan M Davis

June 08, 2017
On Thursday, 8 June 2017 at 02:25:17 UTC, Jonathan M Davis wrote:

Oh I see, the was error related to iteration, not sorting.

> Ranges do not support iterating with an index. The workaround if you want to have an index with ranges and foreach, then you should use lockstep:
>
> http://dlang.org/phobos/std_range.html#lockstep
>
> e.g.
>
> foreach(i, v; lockstep(iota!size_t(0), s))
> {}
>
> or
>
> foreach(i, v; lockstep(iota(0), s))
> {}

There's an enumerate(): https://dlang.org/phobos/std_range.html#enumerate

import std.algorithm : sort;
import std.range : enumerate;

foreach(i, k; aa.keys.sort().enumerate) {
    /* ... */
}
June 07, 2017
On Thursday, June 08, 2017 02:31:43 Stanislav Blinov via Digitalmars-d-learn wrote:
> On Thursday, 8 June 2017 at 02:25:17 UTC, Jonathan M Davis wrote:
>
> Oh I see, the was error related to iteration, not sorting.
>
> > Ranges do not support iterating with an index. The workaround if you want to have an index with ranges and foreach, then you should use lockstep:
> >
> > http://dlang.org/phobos/std_range.html#lockstep
> >
> > e.g.
> >
> > foreach(i, v; lockstep(iota!size_t(0), s))
> > {}
> >
> > or
> >
> > foreach(i, v; lockstep(iota(0), s))
> > {}
>
> There's an enumerate():
> https://dlang.org/phobos/std_range.html#enumerate
>
> import std.algorithm : sort;
> import std.range : enumerate;
>
> foreach(i, k; aa.keys.sort().enumerate) {
>      /* ... */
> }

Even better. I hadn't realized that such a function had been added.

- Jonathan M Davis

June 08, 2017
On Thursday, 8 June 2017 at 01:57:47 UTC, Andrew Edwards wrote:
> Ranges may be finite or infinite but, while the destination may be unreachable, we can definitely tell how far we've traveled. So why doesn't this work?
> ...
> If I hand you a chihuahua for grooming, why am I getting back a pit bull? I simply want a groomed chihuahua. Why do I need to consult a wizard to get back a groomed chihuahua?

A magician should never reveal his secrets, but this I do share.
.sort() returns a SortedRange [1] that encapsulates the range.
The trick is to use .release() to release the controlled range and return it, e.g.

import std.algorithm : sort;
auto keys = aa.keys.sort().release;

[1] https://dlang.org/phobos/std_range.html#SortedRange
« First   ‹ Prev
1 2 3