Thread overview | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 21, 2004 implicit length/end now a must, well almost | ||||
---|---|---|---|---|
| ||||
A bit more DTL ranges, involving slices: foreach(int i; range(-10, 10, +1)[6 .. 12]) { printf("%d ", i); } This prints out -4 -3 -2 -1 0 1 To slice from the beginning, we would start with 0, as in: foreach(int i; range(-10, 10, +1)[0 .. 12]) This prints out -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 But to go to the end from, say, the third element, what do we do? foreach(int i; range(-10, 10, +1)[3 .. ??]) Now of course we can calculate it by the following: foreach(int i; range(-10, 10, +1)[3 .. range(-10, 10, +1).length]) But it's not very succinct, is it? It's pretty confusing to look at for me, so I'm guessing people who aren't familiar with it would have a hard time. Maybe we can use "with", somehow, as in foreach(int i; with range(-10, 10, +1)[3 .. length]) or foreach(int i; with range(-10, 10, +1)[3 .. object.length]) where "object" would be a reserved word and would refer to the current with'd instance. Thoughts? |
March 21, 2004 Re: implicit length/end now a must, well almost | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | [snip] >Maybe we can use "with", somehow, as in > > foreach(int i; with range(-10, 10, +1)[3 .. length]) > >or > > foreach(int i; with range(-10, 10, +1)[3 .. object.length]) > >where "object" would be a reserved word and would refer to the current with'd instance. > >Thoughts? Seems like a simple work-around is to assign the range to a variable and get the length of that. I don't think creating a range and slicing it in the same expression is common. Talking about ranges inspired me to play around with structs and overloading. D is just too much fun. Here's a stab at a range struct for indexing arrays. I hope this is roughly what you had in mind for use cases for ranges. I couldn't find a nice API for overloading the slicing of an arbitrary array type using ranges so I just made it a template and defined some overloaded convience functions. /** A range of ints a, a+step, a+2*step,..., b */ struct range { int a,b,step; /** Constructor (using Stewart Gordon's trick) */ static range opCall(int a,int b,int step) { range t; t.a = a; t.b = b; t.step = step; return t; } /** Constructor with implicit step of 1 */ static range opCall(int a,int b) { return range(a,b,1); } /** Length property */ uint length() { int res = (b-a+1)/step; // overflow? return res>0?res:0; } /** Index operator */ int opIndex(int x) { return a+x*step; } /** Slice operator */ range opSlice(int x, int y) { return range(a+x*step, a+y*step,step); } /** Foreach iteration */ int opApply(int delegate(inout int val) dg) { int res = 0; for (int k=a; k<b; k+=step) { res = dg(k); if (res) break; } return res; } } template TStepSlice(T:T[]) { /** Slice and extract an array x of type T[] by range r */ T[] slice(T[] x, range r) { uint len = r.length; int ind = r.a; T[] res = new T[len]; for (int k; k<len; k++, ind+=r.step ) res[k] = x[ind]; return res; } } // conveniece functions double[] slice(double[] x, range r) { return TStepSlice!(double[]).slice(x,r); } int[] slice(int[] x, range r) { return TStepSlice!(int[]).slice(x,r); } // ...etc int main() { // make the array x we are going to slice double[10] x; for (int k=0;k<10;k++) x[k] = k; // print out x foreach (double val; x) printf("%f ", val); printf("\n"); // slice x using a range double[] y = slice(x,range(0,10,2)); // print out the result foreach (double val; y) printf("%f ", val); printf("\n"); // index into a range range r = range(3,9,2); // 3,5,7 printf("r[1] = %d\n",r[1]); // slice a range range r2 = r[1 .. r.length]; // 5,7 // print out the result foreach (int val; r2) printf("%d ", val); printf("\n"); return 0; } |
March 21, 2004 Re: implicit length/end now a must, well almost | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | On Sun, 21 Mar 2004 09:18:56 +0000, Matthew wrote:
> A bit more DTL ranges, involving slices:
>
> foreach(int i; range(-10, 10, +1)[6 .. 12])
> foreach(int i; range(-10, 10, +1)[3 .. ??])
> foreach(int i; range(-10, 10, +1)[3 .. range(-10, 10, +1).length])
> foreach(int i; with range(-10, 10, +1)[3 .. length])
> foreach(int i; with range(-10, 10, +1)[3 .. object.length])
> Thoughts?
carefull this are my thoughts, you ask for it.
you lost it!
Isn't this a solution looking for a problem?
I notice that again when I browse one of your
columns on the online magazine.
No sane human will code that, it might be usefull
if you are generating code from a program,
but make sure the code generator is so good
that nobody will ever need to maintain the prog.
get a grip
well these are my thoughts. I warn you :)
Ant
|
March 21, 2004 Re: implicit length/end now a must, well almost | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ant |
On Sun, 21 Mar 2004 11:23:59 -0500, Ant <duitoolkit@yahoo.ca> wrote:
>On Sun, 21 Mar 2004 09:18:56 +0000, Matthew wrote:
>
>> A bit more DTL ranges, involving slices:
>>
>> foreach(int i; range(-10, 10, +1)[6 .. 12])
>> foreach(int i; range(-10, 10, +1)[3 .. ??])
>> foreach(int i; range(-10, 10, +1)[3 .. range(-10, 10, +1).length])
>> foreach(int i; with range(-10, 10, +1)[3 .. length])
>> foreach(int i; with range(-10, 10, +1)[3 .. object.length])
>> Thoughts?
>
>carefull this are my thoughts, you ask for it.
>
>you lost it!
>
>Isn't this a solution looking for a problem?
>I notice that again when I browse one of your
>columns on the online magazine.
>
>No sane human will code that, it might be usefull
>if you are generating code from a program,
>but make sure the code generator is so good
>that nobody will ever need to maintain the prog.
>
>get a grip
>
>well these are my thoughts. I warn you :)
>
>Ant
Ouch! harsh words, Ant. I don't agree with everything Matthew proposes (see Vector, etc) but he doesn't deserve to get flamed.
-Ben
|
March 21, 2004 Re: implicit length/end now a must, well almost | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | On Sun, 21 Mar 2004 11:34:51 -0500, Ben Hinkle wrote:
>
> On Sun, 21 Mar 2004 11:23:59 -0500, Ant <duitoolkit@yahoo.ca> wrote:
>
>>On Sun, 21 Mar 2004 09:18:56 +0000, Matthew wrote:
>>
>>> A bit more DTL ranges, involving slices:
>>>
>>> foreach(int i; range(-10, 10, +1)[6 .. 12])
>>> foreach(int i; range(-10, 10, +1)[3 .. ??])
>>> foreach(int i; range(-10, 10, +1)[3 .. range(-10, 10, +1).length])
>>> foreach(int i; with range(-10, 10, +1)[3 .. length])
>>> foreach(int i; with range(-10, 10, +1)[3 .. object.length])
>>> Thoughts?
>>
>>carefull this are my thoughts, you ask for it.
>>
>>you lost it!
>>
>>Isn't this a solution looking for a problem?
>>I notice that again when I browse one of your
>>columns on the online magazine.
>>
>>No sane human will code that, it might be usefull
>>if you are generating code from a program,
>>but make sure the code generator is so good
>>that nobody will ever need to maintain the prog.
>>
>>get a grip
>>
>>well these are my thoughts. I warn you :)
>>
>>Ant
>
> Ouch! harsh words, Ant. I don't agree with everything Matthew proposes (see Vector, etc) but he doesn't deserve to get flamed.
>
> -Ben
Those are my thought, I put the first and last sentence to
try to make it lighter because every thing I write sounds
more harsh then I intended. I just can't write better in
english...
I also put the ":)" at the end...
don't take it that "harsh", Matthew, guys...
Ant
|
March 21, 2004 Re: implicit length/end now a must, well almost | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | "Ben Hinkle" <bhinkle4@juno.com> wrote in message news:tlgr50dv76fdsbll3ro2hvkd8eu3dbfuj8@4ax.com... > > On Sun, 21 Mar 2004 11:23:59 -0500, Ant <duitoolkit@yahoo.ca> wrote: > > >On Sun, 21 Mar 2004 09:18:56 +0000, Matthew wrote: > > > >> A bit more DTL ranges, involving slices: > >> > >> foreach(int i; range(-10, 10, +1)[6 .. 12]) > >> foreach(int i; range(-10, 10, +1)[3 .. ??]) > >> foreach(int i; range(-10, 10, +1)[3 .. range(-10, 10, +1).length]) > >> foreach(int i; with range(-10, 10, +1)[3 .. length]) > >> foreach(int i; with range(-10, 10, +1)[3 .. object.length]) > >> Thoughts? > > > >carefull this are my thoughts, you ask for it. > > > >you lost it! > > > >Isn't this a solution looking for a problem? > >I notice that again when I browse one of your > >columns on the online magazine. > > > >No sane human will code that, it might be usefull > >if you are generating code from a program, > >but make sure the code generator is so good > >that nobody will ever need to maintain the prog. > > > >get a grip > > > >well these are my thoughts. I warn you :) > > > >Ant > > Ouch! harsh words, Ant. I don't agree with everything Matthew proposes (see Vector, etc) but he doesn't deserve to get flamed. Worry not. Thick skin. ;) In fact, Ant's probably got a point about this range thing being a solution looking for a problem. Walter sent me an email earlier suggesting slices on integral ranges, and I did leap straight to thinking about the coding issues discussed before thinking of practical uses. But, to my mind anyway, that's what a NG is for. We fly lots of ideas up, and the good ones survive being shot at. However, I don't agree with that being the assessment of either subjects - the integer to string conversions or the Friendly Templates - from my CUJ online columns so far. Both of these might smell like artificial things - and I've received plenty an email to that effect from some C++ bods - but do they have valid uses. The int2string conversions are super fast and of use in performance-sensitive serialisation (and anything else you need to convert numbers to strings quickly), and the Friendly Templates ... well, you'll have to buy the book to see the use for them, but I promise you it's incredibly useful. Greg Comeau added a new flag to the compiler on the strength of it, which is pretty much all the affirmation I (or anyone else) should really need. :-:) One of the (unsolvable) problems in writing articles is knowing how much context to put in. Whatever you do you'll be accused of wasting time preaching things already known by some and not giving enough context/practical examples by others. I have a good band of chaps whom I consult with my articles (including some compiler vendors...), and their feedback guides me, but at the end of the day you have to judge for yourself. In any case, a column is about stimulating thought about a subject, and they've certainly done that. If you don't want that, don't read it. It doesn't worry me either way. (btw, I'm putting it down to cultural/language differences, but telling me you "warn" me sounds very odd. What are you warning me about? You going to pay me a visit? Very odd ...) Cheers Matthew |
March 21, 2004 Re: implicit length/end now a must, well almost | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ant | "Ant" <duitoolkit@yahoo.ca> wrote in message news:pan.2004.03.21.16.43.50.708896@yahoo.ca... > On Sun, 21 Mar 2004 11:34:51 -0500, Ben Hinkle wrote: > > > > > On Sun, 21 Mar 2004 11:23:59 -0500, Ant <duitoolkit@yahoo.ca> wrote: > > > >>On Sun, 21 Mar 2004 09:18:56 +0000, Matthew wrote: > >> > >>> A bit more DTL ranges, involving slices: > >>> > >>> foreach(int i; range(-10, 10, +1)[6 .. 12]) > >>> foreach(int i; range(-10, 10, +1)[3 .. ??]) > >>> foreach(int i; range(-10, 10, +1)[3 .. range(-10, 10, +1).length]) > >>> foreach(int i; with range(-10, 10, +1)[3 .. length]) > >>> foreach(int i; with range(-10, 10, +1)[3 .. object.length]) > >>> Thoughts? > >> > >>carefull this are my thoughts, you ask for it. > >> > >>you lost it! > >> > >>Isn't this a solution looking for a problem? > >>I notice that again when I browse one of your > >>columns on the online magazine. > >> > >>No sane human will code that, it might be usefull > >>if you are generating code from a program, > >>but make sure the code generator is so good > >>that nobody will ever need to maintain the prog. > >> > >>get a grip > >> > >>well these are my thoughts. I warn you :) > >> > >>Ant > > > > Ouch! harsh words, Ant. I don't agree with everything Matthew proposes (see Vector, etc) but he doesn't deserve to get flamed. > > > > -Ben > > Those are my thought, I put the first and last sentence to > try to make it lighter because every thing I write sounds > more harsh then I intended. I just can't write better in > english... > I also put the ":)" at the end... > > don't take it that "harsh", Matthew, guys... No worries. I've been attacked from the top to the bottom in the last year, so it's cool with me. I think you could probably work on your presentation though, as your post does have pretty unfortunate wording. Were I a bit precious, I might react to the sentiments presented (which I don't imagine are what you meant) rather than the one good point you made, which is that the range thing was probably unnecessary, which I largely agree with. :) |
March 21, 2004 Re: implicit length/end now a must, well almost | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | On Sun, 21 Mar 2004 16:47:58 +0000, Matthew wrote: > > "Ben Hinkle" <bhinkle4@juno.com> wrote in message news:tlgr50dv76fdsbll3ro2hvkd8eu3dbfuj8@4ax.com... >> >> On Sun, 21 Mar 2004 11:23:59 -0500, Ant <duitoolkit@yahoo.ca> wrote: >> >> >On Sun, 21 Mar 2004 09:18:56 +0000, Matthew wrote: >> > >> >> A bit more DTL ranges, involving slices: >> >> >> >> foreach(int i; range(-10, 10, +1)[6 .. 12]) >> >> foreach(int i; range(-10, 10, +1)[3 .. ??]) >> >> foreach(int i; range(-10, 10, +1)[3 .. range(-10, 10, +1).length]) >> >> foreach(int i; with range(-10, 10, +1)[3 .. length]) >> >> foreach(int i; with range(-10, 10, +1)[3 .. object.length]) >> >> Thoughts? >> > >> >carefull this are my thoughts, you ask for it. >> > >> >you lost it! >> > >> >Isn't this a solution looking for a problem? >> >I notice that again when I browse one of your >> >columns on the online magazine. >> > >> >No sane human will code that, it might be usefull >> >if you are generating code from a program, >> >but make sure the code generator is so good >> >that nobody will ever need to maintain the prog. >> > >> >get a grip >> > >> >well these are my thoughts. I warn you :) >> > >> >Ant >> >> Ouch! harsh words, Ant. I don't agree with everything Matthew proposes (see Vector, etc) but he doesn't deserve to get flamed. > > Worry not. Thick skin. ;) I'm relieved :} > > In fact, Ant's probably got a point about this range thing being a solution looking for a problem. you win some... > ...but I promise you it's incredibly useful. you loose some... > (btw, I'm putting it down to cultural/language differences, but telling me you "warn" me sounds very odd. What are you warning me about? You going to pay me a visit? Very odd ...) cultural/language differences? I don't even know what you mean here! What's "a visit" has to do with it? Well, we got the main issues accross. You know you can count on me to speak out my mind :) (or however you say that) Ant |
March 21, 2004 Re: implicit length/end now a must, well almost | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ant | <snip> We're all cool > > (btw, I'm putting it down to cultural/language differences, but telling me > > you "warn" me sounds very odd. What are you warning me about? You going to > > pay me a visit? Very odd ...) > > cultural/language differences? > I don't even know what you mean here! > What's "a visit" has to do with it? > > Well, we got the main issues accross. > You know you can count on me to speak out my mind :) > (or however you say that) He he. It's just when someone says to me "I warn you", that sounds like a threat, so I was jokingly asking whether the threat was to be a physical one, which would require a visit. No matter. :) |
March 21, 2004 Re: implicit length/end now a must, well almost | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ant | Ant wrote:
> On Sun, 21 Mar 2004 09:18:56 +0000, Matthew wrote:
>
>
>>A bit more DTL ranges, involving slices:
>>
>> foreach(int i; range(-10, 10, +1)[6 .. 12])
>> foreach(int i; range(-10, 10, +1)[3 .. ??])
>> foreach(int i; range(-10, 10, +1)[3 .. range(-10, 10, +1).length])
>> foreach(int i; with range(-10, 10, +1)[3 .. length])
>> foreach(int i; with range(-10, 10, +1)[3 .. object.length])
>>Thoughts?
>
> Isn't this a solution looking for a problem?
> I notice that again when I browse one of your
> columns on the online magazine.
In the specific case of range(), maybe. But in the general case, I don't think so; some way to describe the end of an unnamed sequence is definitely needed.
Is there a reason that the endpoints couldn't simply be omitted, like python does? ie blah[5..] slicing everything starting at index 5.
-- andy
|
Copyright © 1999-2021 by the D Language Foundation