std.range.cacheFront proposal&working code: wraps a range to enforce front is called only once

Timothee Cour thelastmammoth at gmail.com
Thu Oct 24 09:56:24 PDT 2013


that changes semantics: what if you want to do some actions before and
after accessing the front element. You'd have to do quite a big of
gymnastics to do that in popFront:
eg:
elements.map!( (a) {preaction(a); auto b=transform(a); postaction(a,b);
return b;}) . filter/sort/map/etc

With what I proposed, it is possible, and it guarantees the lambda will be
called once at most per element.




On Thu, Oct 24, 2013 at 8:55 AM, Chris <wendlec at tcd.ie> wrote:

> On Thursday, 24 October 2013 at 15:40:22 UTC, Dicebot wrote:
>
>> On Thursday, 24 October 2013 at 15:36:59 UTC, Chris wrote:
>>
>>> How do you define side effects? After all ranges manipulate, reformat or
>>> restructure data.
>>>
>>
>> They should do it in popFront. I'd consider any `front` that does
>> anything but accessing cached value suspicious.
>>
>
> So code like
>
> auto front() {
>   doSomething();
>   return range[0];
> }
>
> should go into
>
> void popFront() {
>   doSomething();
>   range = range[1..$];
> }
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20131024/a5ee40d5/attachment.html>


More information about the Digitalmars-d mailing list