Should this work?

Manu turkeyman at gmail.com
Fri Jan 10 17:37:55 PST 2014


On 11 January 2014 05:57, Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org
> wrote:

> On 1/10/14 7:23 AM, Manu wrote:
>
>> This is what I've done. I'm just surprised that such an obvious function
>> doesn't exist, and suspect I was just retarded at phobos again.
>> Having a function that does this is kinda important to simplify lots of
>> expressions that otherwise need to be broken out across a bunch of lines.
>>
>
> I doubt it simplifies a lot.


Well you can pass it as an argument, or use it as a term in an expression
without splitting it across a whole bunch of lines.
Yes, it really does simplify many expressions.

I'm working on something where I go along munching tokens from the stream,
and performing fairly arbitrary sequential logic on them.
My code would be almost twice as long if I needed a second line to advance
the range after each line where I consider the front token's value.

It sucks particularly when there's 'if's involved:

if(r.front == x)
{
  r.popFront();
  x = r.front();
  r.popFront();
}
else
{
  r.popFront();
  y = r.front();
  r.popFront();
}


Surely this is obviously better:

if(r.getAndPopFront() == x)
  x = r.getAndPopFront();
else
  y = r.getAndPopFront();


 Does nobody see this coming up in their code? I have it basically every
>> time I use ranges, and as usual, surprised others don't feel the same way.
>>
>
> If it would have been frequent, it would have been a common request.
> Apparently it isn't. Even before ranges there wasn't a function that got
> you s[0] and also assigned s = s[1 .. $] in one shot, and that wasn't asked
> for either.


I agree. Which is why I asked for it here before writing my own; I assumed
it must already exist, it would have been asked for before...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20140111/2e9c34e7/attachment-0001.html>


More information about the Digitalmars-d mailing list