front evaluated multiple time with joiner depending on where extra arg given

Timothee Cour thelastmammoth at gmail.com
Tue Oct 22 23:41:17 PDT 2013


> In general, if you want to do something once per element which involves
> side
> effects, I would advise using foreach rather than trying to put it into a
> range.


using foreach breaks UFCS chains, and also ElementType != ForeachType


> But if you insist on doing so, the side effect should go in popFront,
> not front, or the side effects will often be happening more than once per
> element.
>

This won't work if side effect should occur _before_ element is popped. And
this is very awkward to use with map/reduce/filter, as it forces one to
write explicitly a range wrapper, defeating purpose of reusing phobos
components. Having to write a custom range (with pop/front/popBack etc)
just to support lambdas with side effects is a pain.

I'd like to propose a generic solution for that, see the email I just sent:

"std.range.cacheFront proposal&working code: wraps a range to enforce front
is called only once"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20131022/91d7b16f/attachment.html>


More information about the Digitalmars-d-learn mailing list