Thread overview | |||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 23, 2005 opApply- and foreach-reverse request | ||||
---|---|---|---|---|
| ||||
I'd like to request an opApply-reverse operator and a new foreach-reverse construct (syntax up for discussion, don't flame me for my -reverse's). Possible syntax: opApplyReverse and foreachreverse (or just foreachr) This would be especially useful for some lists you need to iterate through in reverse. You could have both the forward iterator and reverse iterator versions in the same class. I came across this while coding with DFL. I need to delete the selected items from a ListBox control. The ListBox offers a property called selectedIndices(), and if you delete them in order, all the indices after the one you just deleted become invalid (shifted up by one). The correct way to do it would be to reverse iterate through the selectedIndices and delete them. That way no indices would be invalid after you delete them. Anyone else think this would be a valuable addition to D? Regards, James Dunne |
February 23, 2005 Re: opApply- and foreach-reverse request | ||||
---|---|---|---|---|
| ||||
Posted in reply to James Dunne | On Wed, 23 Feb 2005 23:21:02 +0000 (UTC), James Dunne <jdunne4@bradley.edu> wrote: > I'd like to request an opApply-reverse operator and a new foreach-reverse > construct (syntax up for discussion, don't flame me for my -reverse's). > > Possible syntax: opApplyReverse and foreachreverse (or just foreachr) > > This would be especially useful for some lists you need to iterate through in > reverse. You could have both the forward iterator and reverse iterator versions > in the same class. > > I came across this while coding with DFL. I need to delete the selected items > from a ListBox control. The ListBox offers a property called selectedIndices(), > and if you delete them in order, all the indices after the one you just deleted > become invalid (shifted up by one). The correct way to do it would be to > reverse iterate through the selectedIndices and delete them. That way no > indices would be invalid after you delete them. > > Anyone else think this would be a valuable addition to D? Can't you go.. array = listBox.selectedIndices().dup; foreach(Item i; array.reverse) { } http://www.digitalmars.com/d/arrays.html "reverse" - "Reverses in place the order of the elements in the array. Returns the array." or, are you asking whether we want/need an operator or foreach instead? Regan |
February 24, 2005 Re: opApply- and foreach-reverse request | ||||
---|---|---|---|---|
| ||||
Posted in reply to James Dunne | "James Dunne" <jdunne4@bradley.edu> wrote in message news:cvj34u$2b2s$1@digitaldaemon.com... > I'd like to request an opApply-reverse operator and a new foreach-reverse construct (syntax up for discussion, don't flame me for my -reverse's). > > Possible syntax: opApplyReverse and foreachreverse (or just foreachr) > > This would be especially useful for some lists you need to iterate through > in > reverse. You could have both the forward iterator and reverse iterator > versions > in the same class. > > I came across this while coding with DFL. I need to delete the selected > items > from a ListBox control. The ListBox offers a property called > selectedIndices(), > and if you delete them in order, all the indices after the one you just > deleted > become invalid (shifted up by one). The correct way to do it would be to > reverse iterate through the selectedIndices and delete them. That way no > indices would be invalid after you delete them. > > Anyone else think this would be a valuable addition to D? > > Regards, > James Dunne It would be nice to get something like it. I remember this coming when I was working on the containers in MinTL and so my solution there was to have a property called "backwards" on the container like List that returned a private struct that only implemented foreach (or something like that - I actually can't remember exactly without going and looking it up). So basically the user types List!(int) x; ... foreach(int item; x.backwards) { .... } to iterate backwards over a list. The "reverse" property of List works just like the array reverse property and reverses the list items in-place. I don't know if such a syntax would be a good idea in general. I suppose it depends on exactly how the type of x.backwards would work. Maybe it can have special behavior like "length" where it does magic things in different contexts (ironic that I'm saying this since I'm not a big fan of the magic length behavior). |
February 24, 2005 Re: opApply- and foreach-reverse request | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | In article <opsmocl1lz23k2f5@ally>, Regan Heath says... > >On Wed, 23 Feb 2005 23:21:02 +0000 (UTC), James Dunne <jdunne4@bradley.edu> wrote: >> I'd like to request an opApply-reverse operator and a new foreach-reverse construct (syntax up for discussion, don't flame me for my -reverse's). >> >> Possible syntax: opApplyReverse and foreachreverse (or just foreachr) >> >> This would be especially useful for some lists you need to iterate >> through in >> reverse. You could have both the forward iterator and reverse iterator >> versions >> in the same class. >> >> I came across this while coding with DFL. I need to delete the selected >> items >> from a ListBox control. The ListBox offers a property called >> selectedIndices(), >> and if you delete them in order, all the indices after the one you just >> deleted >> become invalid (shifted up by one). The correct way to do it would be to >> reverse iterate through the selectedIndices and delete them. That way no >> indices would be invalid after you delete them. >> >> Anyone else think this would be a valuable addition to D? > >Can't you go.. > >array = listBox.selectedIndices().dup; >foreach(Item i; array.reverse) { >} Not for a class. dup is a property of arrays. The function selectedIndices() returns a class called SelectedIndexCollection which has opApply overloaded. > >http://www.digitalmars.com/d/arrays.html >"reverse" - "Reverses in place the order of the elements in the array. >Returns the array." > >or, are you asking whether we want/need an operator or foreach instead? > We would need both opApplyReverse and foreachreverse in order to have it; I'm not sure I understand your question. >Regan Regards, James Dunne |
February 24, 2005 Re: opApply- and foreach-reverse request | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | Ben Hinkle wrote:
> List!(int) x;
> ...
> foreach(int item; x.backwards) {
> ....
> }
> to iterate backwards over a list. The "reverse" property of List works just like the array reverse property and reverses the list items in-place.
Sounds like you want the List template to have a backwards method that returns a class that will iterate over the original list backwards. Seems like a simple enough thing to do.
Or do I misunderstand?
John
|
February 24, 2005 Re: opApply- and foreach-reverse request | ||||
---|---|---|---|---|
| ||||
Posted in reply to James Dunne | On Thu, 24 Feb 2005 00:13:19 +0000 (UTC), James Dunne <jdunne4@bradley.edu> wrote: > In article <opsmocl1lz23k2f5@ally>, Regan Heath says... >> >> On Wed, 23 Feb 2005 23:21:02 +0000 (UTC), James Dunne >> <jdunne4@bradley.edu> wrote: >>> I'd like to request an opApply-reverse operator and a new foreach-reverse >>> construct (syntax up for discussion, don't flame me for my -reverse's). >>> >>> Possible syntax: opApplyReverse and foreachreverse (or just foreachr) >>> >>> This would be especially useful for some lists you need to iterate >>> through in >>> reverse. You could have both the forward iterator and reverse iterator >>> versions >>> in the same class. >>> >>> I came across this while coding with DFL. I need to delete the selected >>> items >>> from a ListBox control. The ListBox offers a property called >>> selectedIndices(), >>> and if you delete them in order, all the indices after the one you just >>> deleted >>> become invalid (shifted up by one). The correct way to do it would be to >>> reverse iterate through the selectedIndices and delete them. That way no >>> indices would be invalid after you delete them. >>> >>> Anyone else think this would be a valuable addition to D? > >> >> Can't you go.. >> >> array = listBox.selectedIndices().dup; >> foreach(Item i; array.reverse) { >> } > > Not for a class. dup is a property of arrays. The function selectedIndices() > returns a class called SelectedIndexCollection which has opApply overloaded. Ahh. ok. I see. Ok, in order to be symetrical with an array, should it have a reverse method i.e. c = listBox.selectedIndices().reverse; foreach(Item i; c) { } Granted, a way to iterate backwards, or every 2nd,3rd,4th item .. without having to make a copy of the container would be useful and efficient. >> http://www.digitalmars.com/d/arrays.html >> "reverse" - "Reverses in place the order of the elements in the array. >> Returns the array." >> >> or, are you asking whether we want/need an operator or foreach instead? >> > > We would need both opApplyReverse and foreachreverse in order to have it; I'm > not sure I understand your question. I didn't realise your reply above, my question isn't relevant now :) Regan |
February 24, 2005 Re: opApply- and foreach-reverse request | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Demme | "John Demme" <me@teqdruid.com> wrote in message news:cvj6rk$2e8n$1@digitaldaemon.com... > Ben Hinkle wrote: >> List!(int) x; >> ... >> foreach(int item; x.backwards) { >> .... >> } >> to iterate backwards over a list. The "reverse" property of List works >> just like the array reverse property and reverses the list items >> in-place. > > Sounds like you want the List template to have a backwards method that returns a class that will iterate over the original list backwards. Seems like a simple enough thing to do. > > Or do I misunderstand? > > John Yeah - that's what I did. I was describing how I implemented a similar feature for non-arrays as another data point in the discussion. |
February 24, 2005 Re: opApply- and foreach-reverse request | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | On Wed, 23 Feb 2005 19:53:27 -0500, Ben Hinkle <ben.hinkle@gmail.com> wrote:
> "John Demme" <me@teqdruid.com> wrote in message
> news:cvj6rk$2e8n$1@digitaldaemon.com...
>> Ben Hinkle wrote:
>>> List!(int) x;
>>> ...
>>> foreach(int item; x.backwards) {
>>> ....
>>> }
>>> to iterate backwards over a list. The "reverse" property of List works
>>> just like the array reverse property and reverses the list items
>>> in-place.
>>
>> Sounds like you want the List template to have a backwards method that
>> returns a class that will iterate over the original list backwards. Seems
>> like a simple enough thing to do.
>>
>> Or do I misunderstand?
>>
>> John
>
> Yeah - that's what I did. I was describing how I implemented a similar
> feature for non-arrays as another data point in the discussion.
It seems to me that it doesn't necessarily need to be a different class, it could be another copy of the same class, containing the same info, backwards.
Of course, sometimes a different class is preferrable.
Regan
|
February 24, 2005 Re: opApply- and foreach-reverse request | ||||
---|---|---|---|---|
| ||||
Posted in reply to James Dunne |
James Dunne wrote:
> I'd like to request an opApply-reverse operator and a new foreach-reverse
> construct (syntax up for discussion, don't flame me for my -reverse's).
>
> Possible syntax: opApplyReverse and foreachreverse (or just foreachr)
>
> This would be especially useful for some lists you need to iterate through in
> reverse. You could have both the forward iterator and reverse iterator versions
> in the same class.
>
> I came across this while coding with DFL. I need to delete the selected items
> from a ListBox control. The ListBox offers a property called selectedIndices(),
> and if you delete them in order, all the indices after the one you just deleted
> become invalid (shifted up by one). The correct way to do it would be to
> reverse iterate through the selectedIndices and delete them. That way no
> indices would be invalid after you delete them.
>
> Anyone else think this would be a valuable addition to D?
>
> Regards,
> James Dunne
Norbert & Co, should we have:
foreach (i in m..n) // itreate in no particular order
a[i] = b[i] + 3 * c[i];
in addition to the (quite cool) "existing" vectorising syntax?
(and of course, to avoid hierarchy:)
foreach (i in a..b, j in c..d, k in e..f)
a[i,j,k] = foo_op(x,y,z[j+i+k-2]); // or whatever
|
February 24, 2005 Re: opApply- and foreach-reverse request | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath |
"Regan Heath" <regan@netwin.co.nz> wrote in message news:opsmof4pem23k2f5@ally...
> On Wed, 23 Feb 2005 19:53:27 -0500, Ben Hinkle <ben.hinkle@gmail.com> wrote:
>> "John Demme" <me@teqdruid.com> wrote in message news:cvj6rk$2e8n$1@digitaldaemon.com...
>>> Ben Hinkle wrote:
>>>> List!(int) x;
>>>> ...
>>>> foreach(int item; x.backwards) {
>>>> ....
>>>> }
>>>> to iterate backwards over a list. The "reverse" property of List works
>>>> just like the array reverse property and reverses the list items
>>>> in-place.
>>>
>>> Sounds like you want the List template to have a backwards method that
>>> returns a class that will iterate over the original list backwards.
>>> Seems
>>> like a simple enough thing to do.
>>>
>>> Or do I misunderstand?
>>>
>>> John
>>
>> Yeah - that's what I did. I was describing how I implemented a similar feature for non-arrays as another data point in the discussion.
>
> It seems to me that it doesn't necessarily need to be a different class, it could be another copy of the same class, containing the same info, backwards.
>
> Of course, sometimes a different class is preferrable.
>
> Regan
Two points:
1) it's actually a struct so that no garbage is created by foreaching
backwards. If I had to choose between writing a for loop that steps
backwards and writing a foreach loop that generating garbage I'd choose
writing the for loop - but that's me.
2) a different type is needed because foreach on a regular List means
forwards foreach and I decided that storing a flag as part of a general List
just for backwards traversals wasn't worth the storage space.
The actual implementation factors out the for loop into a function that the regular List calls with a step of +1 and the BackwardsList (or whatever name I used) calls with step -1.
|
Copyright © 1999-2021 by the D Language Foundation