<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 14 January 2014 19:04, Manu <span dir="ltr"><<a href="mailto:turkeyman@gmail.com" target="_blank">turkeyman@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><div class="h5">On 14 January 2014 18:36, Jakob Ovrum <span dir="ltr"><<a href="mailto:jakobovrum@gmail.com" target="_blank">jakobovrum@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>On Tuesday, 14 January 2014 at 08:23:05 UTC, Manu wrote:<br>
</div><div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
1. A termination condition (ie, while)<br>
<br>
foreach(t; things) iterates each thing, but it's common in traditional for<br>
loops to have an && in the second 'while' term, to add an additional<br>
termination condition.<br>
for(i=0; i<things.length && things[i].someCondition; ++i)<br>
<br>
Or with foreach:<br>
foreach(i, t; things)<br>
{<br>
  if(t.someCondition)<br>
    break;<br>
  ...<br>
}<br>
</blockquote>
<br></div>
foreach(t; things.until!(t => t.someCondition))<br>
{<br>
}<br>
<br>
Unfortunately foreach over a range does not automatically support an index loop variable. We could add something like std.range.enumerate to support this, but I think it's a common enough requirement that a language amendment is warranted (there are some subtleties involved in implementing it though - specifically when combined with automatic tuple expansion).<div>

<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
2. A filter<br>
<br>
The other thing is the ability to skip uninteresting elements. This is<br>
typically performed with the first line of the loop testing a condition,<br>
and then continue:<br>
foreach(i, t; things)<br>
{<br>
  if(!t.isInteresting)<br>
    continue;<br>
  ...<br>
}<br>
</blockquote>
<br></div>
foreach(t; things.filter!(t => t.isInteresting))<br>
{<br>
}<br>
<br>
Ditto about the index loop variable.<div><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I've tried to approach the problem with std.algorithm, but I find the<br>
std.algorithm statement to be much more noisy and usually longer when the<br>
loops are sufficiently simple (as they usually are in my case, which is why<br>
the trivial conditions are so distracting by contrast).<br>
</blockquote>
<br></div>
The two examples above look a *lot* cleaner and less noisy (declarative!) to me than the imperative approach using if-break or if-continue.</blockquote><div><br></div></div></div><div>/agree completely.</div><div>This is nice, I didn't think of writing statements like that :)</div>

<div>That's precisely the sort of suggestion I was hoping for. I'll continue like this.</div></div></div></div>
</blockquote></div><br></div><div class="gmail_extra">Can anyone comment on the codegen when using these statements? Is it identical to my reference 'if' statement?</div><div class="gmail_extra">Liberal use of loops like this will probably obliterate unoptimised performance... :/<br>
</div></div>