std.algorithm.joiner unexpected behavior
H. S. Teoh via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Aug 31 14:09:55 PDT 2017
On Thu, Aug 31, 2017 at 01:34:39PM -0600, Jonathan M Davis via Digitalmars-d-learn wrote:
[...]
> In general, byLine does not work with other range-based algorithms
> precisely because it reuses the buffer. I think that it does manage to
> work for some, but IMHO, it should have just supported foreach via
> opApply and not been a range at all. It's great for efficiency in a
> loop but horrible for range chaining.
[...]
Transient ranges are tricky to work with, I agree, but I don't agree
that they're "horrible for range chaining". I argue that many range
algorithms that assume the persistence of .front are inherently wrong,
and ought to be implemented in a way that does *not* make this
assumption. Many std.algorithm algorithms *can* in fact be written in
this way, and those that aren't, are arguably buggy.
For example, some std.algorithm functions take a forward range but then
tries to save .front to a local variable. Rather, they should use .save
to save the previous position of the range so that they can call .front
on that to access the previous element, instead of making the unfounded
assumption that whatever the local variable refers to will still remain
valid after calling .popFront. It's just sloppy coding IMNSHO.
T
--
MS Windows: 64-bit rehash of 32-bit extensions and a graphical shell for a 16-bit patch to an 8-bit operating system originally coded for a 4-bit microprocessor, written by a 2-bit company that can't stand 1-bit of competition.
More information about the Digitalmars-d-learn
mailing list