December 12, 2010 Re: _indexed_ iteration using opApply or range -- PS | ||||
---|---|---|---|---|
| ||||
On Sun, 12 Dec 2010 20:47:04 +0100
spir <denis.spir@gmail.com> wrote:
> Hello,
>
> Had a nice time figuring out how to let opApply allow index iteration like:
> foreach (i, x ; collection) {}
> Finally managed to do it adding 'i' everywhere:
>
> struct S1 {
> private int[] elements = [];
> int opApply (int delegate (ref uint, ref int) block) {
> foreach (uint i, int n ; this.elements)
> block(i, n);
> return 0;
> }
> }
>
> Is this the intended idiom?
> Now, I'm trying to make this work with ranges instead (input range only as of now). Seems i'm not smart enough to guess it alone...
>
> Thank you,
> Denis
PS: Also tried to make iteration work by defining slice "with no argument" (coll[]) only, like indicated in TDPL p.381. But could not manage to do it.
Thank you again,
Denis
-- -- -- -- -- -- --
vit esse estrany ☣
spir.wikidot.com
|
December 12, 2010 Re: _indexed_ iteration using opApply or range -- PS | ||||
---|---|---|---|---|
| ||||
Posted in reply to spir | spir <denis.spir@gmail.com> wrote: > PS: Also tried to make iteration work by defining slice "with no argument" (coll[]) only, like indicated in TDPL p.381. But could not manage to do it. The point of coll[] is that a collection may not want to define the range primitives itself, but rather expose a special range struct for that purpose: class Collection { auto opSlice( ) { struct range { // front, popFront, empty, whosYourDaddy } return range( this ); } } -- Simen |
Copyright © 1999-2021 by the D Language Foundation