Should this work?

Marco Leise Marco.Leise at gmx.de
Sat Jan 11 06:14:08 PST 2014


Am Sat, 11 Jan 2014 10:40:25 +1000
schrieb Manu <turkeyman at gmail.com>:

> On 11 January 2014 01:20, John Colvin <john.loughran.colvin at gmail.com>wrote:
> 
> > On Friday, 10 January 2014 at 15:19:39 UTC, John Colvin wrote:
> >
> >> On Friday, 10 January 2014 at 15:05:18 UTC, monarch_dodra wrote:
> >>
> >>> On Friday, 10 January 2014 at 14:31:31 UTC, John Colvin wrote:
> >>>
> >>>> or if you want something short and simple, define a free function:
> >>>> auto popFrontRet(R)(ref R range)
> >>>>   if(isInputRange!R)
> >>>> {
> >>>>   range.popFront();
> >>>>   assert(!range.empty);
> >>>>   return range.front;
> >>>> }
> >>>>
> >>>
> >>> *Unless* I'm mistaken, he was asking for something that returns the
> >>> *popped* element?
> >>>
> >>> Re-reading the question, it does kind of sound a bit ambiguous now.
> >>>
> >>
> >> Woops, of course:
> >>
> >> auto popFrontRet(R)(ref R range)
> >>     if(isInputRange!R)
> >> {
> >>     auto tmp = range.front;
> >>     range.popFront();
> >> }
> >>
> >>
> >> That also invalidates my second point to do with emptiness.
> >>
> >
> > ugh, today is not my day:
> >
> >  auto popFrontRet(R)(ref R range)
> >>     if(isInputRange!R)
> >> {
> >>     auto tmp = range.front;
> >>     range.popFront();
> >>     return tmp;
> >> }
> >>

This returns garbage for ranges that reuse their internal
buffer for performance reasons. E.g. byLine will overwrite
what tmp points to in the call to popFront();

> Yes, this is what I did. It should be added to phobos, but I figured
> there's a reason it's not there...
> My point was, I want this more often than I want either of those other
> primitives. I'm surprised it doesn't exist, and suspected I was just being
> blind again.
> 
> I also wrote popFrontRetN to capture more than one element from the start.
 
What about take(n) or take(n).array() ?

-- 
Marco



More information about the Digitalmars-d mailing list