<div dir="ltr">So, the last few days, 2 things have been coming up constantly.<div><br></div><div>foreach is like, the best thing about D. But I often get caught by 2 little details that result in my having to create a bunch more visual noise.</div>
<div><br></div><div>1. A termination condition (ie, while)</div><div><br></div><div>foreach(t; things) iterates each thing, but it's common in traditional for loops to have an && in the second 'while' term, to add an additional termination condition.</div>
<div>for(i=0; i<things.length && things[i].someCondition; ++i)</div><div><br></div><div>Or with foreach:</div><div>foreach(i, t; things)</div><div>{</div><div>  if(t.someCondition)</div><div>    break;</div><div>
  ...</div><div>}</div><div><br></div><div>I often feel like I want something like this where I would have the opportunity to add that additional term I lose with a traditional for loop:</div><div>  foreach(i, t; things; t.someCondition)</div>
<div><br></div><div><br></div><div>2. A filter</div><div><br></div><div>The other thing is the ability to skip uninteresting elements. This is typically performed with the first line of the loop testing a condition, and then continue:</div>
<div>foreach(i, t; things)</div><div>{</div><div>  if(!t.isInteresting)</div><div>    continue;</div><div>  ...</div><div>}</div><div><br></div><div><br></div><div>I'm finding in practise that at least one of these seems to pop up on the vast majority of loops I'm writing. It produces a lot of visual noise, and I'm finding it's quite a distraction from the otherwise relative tidiness of my code.</div>
<div><br></div><div>How have others dealt with this? I wonder if it's worth exploring a 3rd term in the foreach statement?</div><div><br></div><div>I've tried to approach the problem with std.algorithm, but I find the std.algorithm statement to be much more noisy and usually longer when the loops are sufficiently simple (as they usually are in my case, which is why the trivial conditions are so distracting by contrast).</div>
<div>I also find that the exclamation mark overload in typical std.algorithm statements tends to visually obscure the trivial condition I'm wanting to insert in the first place.</div><div>It's also really hard to debug std.algorithm statements, you can't step the debugger anymore.</div>
<div><br></div><div>Also, I have to import std.algorithm, which then imports the universe... >_<</div></div>