August 25, 2013
On Sunday, 25 August 2013 at 21:01:54 UTC, bearophile wrote:
>> [* Hijacking of discussion: a while back I think I floated the idea of generalizing iota() with closed/open boundary conditions similar to those found in std.random.uniform; so e.g. you could do iota!"[]"(0, 10) and the upper bound would be included in the values returned.  Would be useful for cases like these.]
>
> Yes, it's a kind of necessary enhancement:
> http://d.puremagic.com/issues/show_bug.cgi?id=10466
>
> Bye,
> bearophile

If somebody were to implement this, I would *love* to review it.
August 25, 2013
On 08/25/2013 10:42 PM, Joseph Rushton Wakeling wrote:
>
>
>      foreach (immutable i; iota(0, n, 2)) { ... }
>
> ... but again, that will be slow compared to the for loop or a foreach
> across an interval.

Why would that be the case?

August 25, 2013
On 8/25/13 12:46 PM, Joseph Rushton Wakeling wrote:
> On 25/08/13 21:10, monarch_dodra wrote:
>> It never clashes untill you nest two foreach, and then you have to use
>> __ ...
>>
>> foreach( _ ; 0 .. M)
>>      foreach( __ ; 0 .. N)
>>          ...
>>
>> I have an enhancement request to simply allow anonymous iteration:
>> foreach( ; 0 .. N)
>>
>> http://d.puremagic.com/issues/show_bug.cgi?id=9009
>
> Good call. :-)

I don't see why the excitement around anonymous bindings. It's a rare case and it's not like we're running out of symbols, particularly given they're by definition written only once :o).

Andrei

August 25, 2013
On Sun, Aug 25, 2013 at 11:10:39PM +0200, monarch_dodra wrote:
> On Sunday, 25 August 2013 at 21:01:54 UTC, bearophile wrote:
> >>[* Hijacking of discussion: a while back I think I floated the idea of generalizing iota() with closed/open boundary conditions similar to those found in std.random.uniform; so e.g. you could do iota!"[]"(0, 10) and the upper bound would be included in the values returned.  Would be useful for cases like these.]
> >
> >Yes, it's a kind of necessary enhancement: http://d.puremagic.com/issues/show_bug.cgi?id=10466
> >
> >Bye,
> >bearophile
> 
> If somebody were to implement this, I would *love* to review it.

If somebody were to attempt to implement this, I'd recommend taking this issue into account as well:

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

	(which is a generalization of:
	http://d.puremagic.com/issues/show_bug.cgi?id=6447)

I don't know if the total amount of changes would warrant rewriting iota entirely in order to keep the complexity of the implementation minimal.

(The nice thing about D unittests is that rewriting code is much less scary because if the unittests are complete enough, they will catch any regressions right off the bat.)


T

-- 
WINDOWS = Will Install Needless Data On Whole System -- CompuMan
August 25, 2013
Andrei Alexandrescu:

> I don't see why the excitement around anonymous bindings. It's a rare case and it's not like we're running out of symbols, particularly given they're by definition written only once :o).

It's not a rare case, I can test this searching in my code for "foreach (_;".

And while not essential, anonymous loops are useful because adding an useless variable to your code increases its complexity a little without giving something back. It's just like adding an useless variable inside one of your functions. In C/C++ I look for the "unused variable" warnings (a warning that is currently missing in D).

When you define a function, and you don't want to use its second argument, D allows you to not give it a name, despite you are not running out of symbols:

void foo(int x, int) {}

When I see code with a loop with a variable, to understand the loop I first of all search how such variable is used inside the loop. If the loop variable is anonymous this tells me something important about the loop, and reduces the noise.

Bye,
bearophile
August 25, 2013
Timon Gehr:

>> ... but again, that will be slow compared to the for loop or a foreach
>> across an interval.
>
> Why would that be the case?

In the real world, where we live, there is a thing named "abstraction penalty", it's something that asks you to pay something when there is an interface between separated subsystems. To avoid paying that penalty at run-time in your programs you need a smarter compiler, that usually requires more time to be developed, and usually requires more time to do its work (this means slower compilations, see the LDC2 compiler compared to DMD. LDC2 was able to remove most of the run-time penalty, but when it compiles it's slower than DMD). So in the real life we have to take compromises all the time.

Bye,
bearophile
August 25, 2013
On 25/08/13 23:11, Timon Gehr wrote:
> Why would that be the case?

I don't know why it _need_ be the case, but it _is_ the case in practice right now.

The precise example I cited of a for() loop with decreasing value, I tried replacing using retro and iota -- and it was significantly slower.

Ditto foreach's over iota() -- even though in principle there is no conceptual difference between foreach(i; m .. n) and foreach(i; iota(m, n)), and it ought to be possible for the two to reduce to the same machine code, there is right now a performance hit.
August 25, 2013
On 25/08/13 23:17, Andrei Alexandrescu wrote:
> I don't see why the excitement around anonymous bindings. It's a rare case and
> it's not like we're running out of symbols, particularly given they're by
> definition written only once :o).

Every little helps. :-)

(Don't know if you get the cultural reference on that, it probably helps to be British...:-)

August 25, 2013
> It's not a rare case, I can test this searching in my code for "foreach (_;".

Beside my code, if you want I can show 16 URLs to use cases of "anonymous iteration" in D code.

Bye,
bearophile
August 25, 2013
On 25/08/13 23:01, bearophile wrote:
> Thankfully foreach_reverse was not deprecated:

Really?  I thought it was viewed these days as some kind of awful abomination that should never have been in the language.  (I'm not sure I understand why, but equally I'm not sure I care to open the can of worms of finding out why...)