random access-range without lower-power range kinds?
spir
denis.spir at gmail.com
Tue Dec 14 08:32:49 PST 2010
On Tue, 14 Dec 2010 14:15:20 +0000 (UTC)
"Lars T. Kyllingstad" <public at kyllingen.NOSPAMnet> wrote:
> On Tue, 14 Dec 2010 09:09:33 +0100, spir wrote:
>
> > Hello,
> >
> > It seems impossible to define a random-access range (opIndex + length)
> > alone. In fact, I cannot have it used by the language. Am I missing
> > something? Random-access looks enough to provide fonctionality for both
> > input and bidirectional ranges without any additional method. "Lowering"
> > for forward iteration means I guess ;-)
> > for (uint i=0 ; i < coll.length ; i++) {
> > element = coll[i];
> > doSomethingWith(element);
> > }
> > What is the reason for requiring methods of lower-power range types to
> > be defined? (This makes 5 methods!)
> >
> > Denis
> > -- -- -- -- -- -- --
> > vit esse estrany ☣
> >
> > spir.wikidot.com
>
>
> To avoid the boilerplate, you could write a mixin that defines the
> iteration primitives for you.
>
> mixin template IterationFuncs()
> {
> int index;
> bool empty() { return index == length; }
> auto front() { return opIndex(index); }
> void popFront() { ++index; }
> // ... etc.
> }
>
> Then you'd just have to define opIndex() and length(), and the mixin does
> the rest for you.
>
> struct MyRange(T)
> {
> T opIndex(int i) { ... }
> @property int length() { ... }
> mixin IterationFuncs!();
> }
>
> (I haven't tested the code above, so it probably has bugs, but you get
> the point.)
>
> -Lars
Thank you, Lars. Nice method!
Denis
-- -- -- -- -- -- --
vit esse estrany ☣
spir.wikidot.com
More information about the Digitalmars-d-learn
mailing list