F
Fabio Cannizzo
Is it possible to do something similar to the STL iterators, i.e. to execute
a foreach loop in reverse order?
Thanks,
Fabio
a foreach loop in reverse order?
Thanks,
Fabio
Is it possible to do something similar to the STL iterators, i.e. to
execute a foreach loop in reverse order?
Fabio Cannizzo said:Is it possible to do something similar to the STL iterators, i.e. to execute
a foreach loop in reverse order?
Another example - consider an iterator which yielded prime numbers, in
order. What would a reversed iterator do?
Peter said:Easy - start from the highest prime number, and work downwards. Was it a
trick question?
Jon Skeet said:No - and not every iterator would be able to support that behaviour
anyway. Consider a foreach which was based off a stream coming in from
a network - you'd have to wait to find the end of the stream (if that
ever happened) before you could start giving back numbers.
Jon Skeet said:I'm not sure whether you're being sarcastic or not - apologies if you
were joking,
Fabio Cannizzo said:No - and not every iterator would be able to support that behaviour
anyway. Consider a foreach which was based off a stream coming in from
a network - you'd have to wait to find the end of the stream (if that
ever happened) before you could start giving back numbers.
Another example - consider an iterator which yielded prime numbers, in
order. What would a reversed iterator do?
chris martin said:I'm assuming you are talking about something other than executing a foreach
in reverse.
Why wouldn't this work if that's not the case.
Fabio Cannizzo said:I think Peter has a point. In the computer world the concept of infinite
does not exist.
Be it a set, an array or a collection, at any particular point in time it
will have a finite number of elements.
I understand that in C# these do not exists, but I do not agree they do not
make sense. The STL succesfully and conveninetly implement them.
chris martin said:Because your code requires that what you want to enumerate implements
IList. Not everything which implements IEnumerator or IEnumerable
implements IList.
<snip>
Jon Skeet said:Because your code requires that what you want to enumerate implements
IList. Not everything which implements IEnumerator or IEnumerable
implements IList.
Nick said:Because your code requires that what you want to enumerate implements
IList. Not everything which implements IEnumerator or IEnumerable
implements IList.
It is totally possible to write a wrapper that can take IList,IList<T> or
anything (by reflection) that includes appropriate methods/properties (these
would be this[] and Count).
easy to change to do IList said:You could even allow things having Count and
implementing IEnumerable although it wouldn't be efficient.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.