Re: [Feature Request] "first:" "last:" sections in a "foreach" block
From: Daniel O'Connell [C# MVP] (onyxkirx_at_--NOSPAM--comcast.net)
Date: 04/05/04
- Next message: Eric Cadwell: "Re: How to have access to an element array ?"
- Previous message: Andreas Håkansson: "Re: Mutex and messages"
- In reply to: Joep: "Re: [Feature Request] "first:" "last:" sections in a "foreach" block"
- Next in thread: SpookyET: "Re: [Feature Request] "first:" "last:" sections in a "foreach" block"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 5 Apr 2004 18:17:19 -0500
"Joep" <Staat@DeStoep.nl> wrote in message
news:4071d714$0$559$e4fe514c@news.xs4all.nl...
> IMHO
>
> foreach means foreach means foreach, so for each item of a collection of
> items do this. I would not like to see a switch inside the loop that test
> for the first or the last or whichever item. It conflicts with what I
> learned long time ago about loop-unrolling and its effect.
>
> Instead, I would like to state foreach item in this (not that) collection
> do
> this, this collection excluding the first and last item for example, in
> other words, I would like to be able to more precisely define the items
> for
> which I want some code to execute. It could be that I want to execute this
> for every other item and that for all the others.
>
> What about something along these lines where I would be able to define a
> part of the collection of items? A bit like string.substring and similar
> constructs.
>
> foreach(item in items[2,8])
>
> or
>
> foreach(item in items[2,])
>
> or like the Python splice thingy?
A few problems with this.
Order returned by an enumerator is only statically defined during the
lifetime of the enumerator. There is nothing that requires two sequential
executions of foreach over the same object to actually process the items in
the same order(or really that IEnumerator isn't strictly required to
maintain predictable order). In some situations, say when used with IList, a
natural order can be established, but in other cases when you are
considering a tree or a hashtable or an enumerator that generates its
results there may not be a natural order and ordering *may* be
unpredictable. In which case it wouldn't be possible to achieve the original
point of the feature propsal.
Another is that, unless you happen to know the length of the enumerator,
this syntax doesn't help you in the least with determining when the last
item occurs. Again, in data structures its probable that you'll get a count,
but its certainly not definate.
Yet another, somewhat related to the above, is that foreach (x in
enumerator[1]) isn't particularly sensible but it would have to be done
unless you wanted to directly access the enumerator. Also when you consider
index 0 may not be the first value returned by the IEnumerator you can't
even rely on an indexer in all cases(assuming one even exists).
>
> The suggestion I like, meaning a way to more precisely define which items
> to
> execute code for. Syntax and semantics are another thing.
This suggestion is interesting, but it simply isn't the same as the original
proposal. This particular syntax allows you to specify a range to exectue
over while the original proposed a way to execute code based entirely on the
order in the enumeration, not the list or whatever the values are being read
from. The syntax proposed could operate with what you proposed pretty simply
foreach (char x in str[1,4])
{
case first: Console.WriteLine("First: " + x); //matches the first value,
in this case str[1]
case last: Console.WriteLine("Last: " + x); //matchs the last value, in
this case str[4]
case other: Console.WriteLine("Middle: " + x); //matches all cases other
than str[4] and str[5]
}
Of course, new syntax needs to exist. The indexer accessor would be
conflicting with actual indexers(Imagine an indexer that returns an
IEumerable).
>
> Can't this be done already I wonder...
In theory you could do it with a custom IEnumerable\IEnumerator pair that
wraps an existing IEnumerator, but there is no syntax I nkow of.
- Next message: Eric Cadwell: "Re: How to have access to an element array ?"
- Previous message: Andreas Håkansson: "Re: Mutex and messages"
- In reply to: Joep: "Re: [Feature Request] "first:" "last:" sections in a "foreach" block"
- Next in thread: SpookyET: "Re: [Feature Request] "first:" "last:" sections in a "foreach" block"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|