June 08, 2017
On Wed, 2017-06-07 at 19:39 -0700, Jonathan M Davis via Digitalmars-d- learn wrote:
> 
[…]
> Even better. I hadn't realized that such a function had been added.
> 

Another import from Python. :-)

-- 
Russel. ============================================================================= Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder@ekiga.net 41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel@winder.org.uk London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

June 08, 2017
On Thu, 2017-06-08 at 00:23 -0700, Jonathan M Davis via Digitalmars-d- learn wrote:
> […]
> 
> release is a member of SortedRange. You don't have to import it
> separately.
> You have it automatically by virtue of the fact that sort returns a
> SortedRange. And unlike calling array, it doesn't copy the entire
> range or
> allocate.
> 
[…]

I will have to admit, that on my recent return to more full time D programming, that using both class members, and imported functions with UFCS, can lead to annoyance of understanding. I can't put my finger on what paragraph of documentation would help just yet though.

-- 
Russel. ============================================================================= Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder@ekiga.net 41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel@winder.org.uk London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

June 08, 2017
On 6/7/17 9:57 PM, 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?

The issue here is that arrays are special. Arrays allow foreach(i, v; arr) and foreach(v; arr). Ranges in general do not. So there is no way to forward this capability via the range interface. Not only that, but foreach(i, v; arr) is much different than iterating even a range that returns a tuple in that the index is *specific to the loop* and doesn't involve the array at all. It's more similar to opApply.

opApply can help, but still is different than how arrays work (foreach with array is handled directly by the compiler). But ranges are not going to be forwarding this feature just for arrays.

In answer to your question, if we returned something that *didn't* take advantage of the fact that the range is now sorted, then people who want that functionality (e.g. builtin binary search) would be complaining.

As a recommendation, I suggest you get used to using enumerate, as it will work for all cases of ranges, including arrays.

-Steve
June 08, 2017
On 08.06.2017 14:06, Steven Schveighoffer wrote:
> 
> The issue here is that arrays are special. Arrays allow foreach(i, v; arr) and foreach(v; arr). Ranges in general do not. So there is no way to forward this capability via the range interface. Not only that, but foreach(i, v; arr) is much different than iterating even a range that returns a tuple in that the index is *specific to the loop* and doesn't involve the array at all. It's more similar to opApply.
> 
> opApply can help, but still is different than how arrays work (foreach with array is handled directly by the compiler). But ranges are not going to be forwarding this feature just for arrays.

Arguably, foreach over ranges and foreach over arrays are badly designed. Automatic tuple expansion only works with ranges, and indexed iteration only works with arrays, and they are mutually incompatible. Therefore, there is no good way to fix the problem.
1 2 3
Next ›   Last »