Python style reversed iteration (http://www.python.org/dev/doc/devel/whatsnew/node7.html).
Perhaps a more compact syntax
for i in reversed(xrange(1,4)):
... print i
rfor i in xrange(1,4):
I don't think adding a new keyword would be necessary - at least for now, while we don't have macros. Something like reverse(<list>) would be a lot simpler - or even, why not just xrange(4,1)?
a function is applicable to a wider range of situations than a explicit reversed loop operation:
lastLine, lineBeforeLast = reversed(File.OpenText("foo.txt"))
let's make the 0.5 release sooner.
The only way I can think to implement reverse iteration is to first iterate through the array, then pass back a new enumerator (ReversedEnumerator) that pops out the values last to first. However, the inital iteration to gather all the values might actually affect or trigger something.
Can anyone think of a way of reverse iteration in C# that does not require going through the the enumeration one way, first?
This is a tentative patch that implements a basic builtin function called "reversed." It takes one parameter, an object that implements IEnumerable, then reverses the contants (iterating through the IEnumerable object once), and returns an IEnumerable (of the reversed loot).
Its probably suboptimal, but it is the only way I could really think of implementing it.
This patch sucks significantly less--it only takes about 0.5 seconds to reverse an enumerable object of 1,000 items, which is likely the most anyone will ever use it for.
I wipe my hands of it!
(I am a machochist)
patch applied. thanks!