Re: [Feature Request] "first:" "last:" sections in a "foreach" block

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Daniel O'Connell [C# MVP] (onyxkirx_at_--NOSPAM--comcast.net)
Date: 04/05/04


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.



Relevant Pages

  • Re: foreach vs. for
    ... >> to be either reading to or writing from the array. ... >> comparing foreach with for, then due to the limitations of foreach, you ... because the Enumerator definition is only as Object. ... > optimizations made by the C# compiler. ...
    (microsoft.public.dotnet.framework.performance)
  • Re: foreach vs. for
    ... >> to be either reading to or writing from the array. ... >> comparing foreach with for, then due to the limitations of foreach, you ... because the Enumerator definition is only as Object. ... > optimizations made by the C# compiler. ...
    (microsoft.public.dotnet.general)
  • Re: foreach or List.ForEach
    ... Not sure what you mean by that, ForEach doesn't allocate enumerators, while ... foreach on the list allocates an enumerator and calls Dispose once per ... special profiler using the HW CPU and memory controller perfcounters). ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Question about Iteration and forEach
    ... It's not at all clear to me that simply "peeking" at the next element in the iteration would be sufficient for the original poster. ... --- The original poster said "I would like to test the next item but if it’s not the one that I want how do I put it back so that when my foreach continues it is in the next iteration". ... to me that it would be simpler to simply emulate a "peekable" enumerator through something like this: ... spend plenty of time offering solutions to others, and in fact very little of my time is spent "knocking others for actually trying". ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: foreach enhancement
    ... foreach ) ... if you can extend the concept out to inline array expressions or other ... Consider something like(these are all psuedo syntax, ... >> And maybe we could also try to enhance the loop further and make it ...
    (microsoft.public.dotnet.languages.csharp)