digitalmars.D.learn - random access-range without lower-power range kinds?
- spir (16/16) Dec 14 2010 Hello,
- Lars T. Kyllingstad (22/41) Dec 14 2010 To avoid the boilerplate, you could write a mixin that defines the
- spir (8/57) Dec 14 2010 =20
- Jesse Phillips (3/29) Dec 14 2010 Maybe this should be added to std.range?
Hello, It seems impossible to define a random-access range (opIndex + length) alon= e. 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 bidi= rectional ranges without any additional method. "Lowering" for forward iter= ation means I guess ;-) for (uint i=3D0 ; i < coll.length ; i++) { element =3D coll[i]; doSomethingWith(element); } What is the reason for requiring methods of lower-power range types to be d= efined? (This makes 5 methods!) Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Dec 14 2010
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.comTo 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
Dec 14 2010
On Tue, 14 Dec 2010 14:15:20 +0000 (UTC) "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> wrote:On Tue, 14 Dec 2010 09:09:33 +0100, spir wrote: =20=20Hello, =20 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=3D0 ; i < coll.length ; i++) { element =3D coll[i]; doSomethingWith(element); } What is the reason for requiring methods of lower-power range types to be defined? (This makes 5 methods!) =20 Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 =20 spir.wikidot.com=20 =20 To avoid the boilerplate, you could write a mixin that defines the=20 iteration primitives for you. =20 mixin template IterationFuncs() { int index; bool empty() { return index =3D=3D length; } auto front() { return opIndex(index); } void popFront() { ++index; } // ... etc. } =20 Then you'd just have to define opIndex() and length(), and the mixin does=the rest for you. =20 struct MyRange(T) { T opIndex(int i) { ... } property int length() { ... } mixin IterationFuncs!(); } =20 (I haven't tested the code above, so it probably has bugs, but you get=20 the point.) =20 -LarsThank you, Lars. Nice method! Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Dec 14 2010
Lars T. Kyllingstad Wrote: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.) -LarsMaybe this should be added to std.range? http://d.puremagic.com/issues/show_bug.cgi?id=5351
Dec 14 2010