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

From: James Curran (JamesCurran_at_mvps.org)
Date: 04/04/04


Date: Sun, 4 Apr 2004 08:17:46 -0700


"Paul E Collins" <find_my_real_address@CL4.org> wrote in message
news:c4ovj5$ij8$1@hercules.btinternet.com...
> 'foreach' and indexed 'for' are pretty much interchangeable.

    Actually they aren't. foreach works for every class that implements
IEnumerable, which is small and easy to implement. Indexing requires
implementing IList which is more complicated. Many collection
implementations don't lend themselves well to indexing (anything which using
a tree instead of an array behind the scenes). Check out their desciption
pages in the MSDN. In the framework over twice as many classes implement
IEnumerable as IList.

>> I'm somewhat influenced by the fact that I tend to use 'foreach' when I
want
to iterate over all of the elements but don't care which order they come up
in <<

Just because there is no particular order to the items in the collection,
does not mean when being processed, the first and last do not need to be
processed differently. As in my example of adding the comma, often it's just
the fact that this item is the first processed is enough to require special
processing.

>What if it's the *second* iteration you want to treat differently;
> should there be a 'second' keyword as well, or some kind of 'foreach ...
> case' construct?

No. Needing to treat the first or last item differently is fairly common.
Needing to treat an item besides the first or last differently is much
rarer. Also, there is no simple implementation for treating a middle item
differently.

I guess the basic question is what is the point of having a foreach in the
language anyway, when it can always be replaced by a for or while statement?
If you see an advantage in having it in the language, you should see the
advantage of this.



Relevant Pages

  • Re: "C vs java"
    ... The first obvious error is the confusion of implementations with the ... language proper, in the "compilation" row. ... The "array declarations" row doesn't show how to declare an array. ... realloc but also static declaration syntax. ...
    (comp.lang.c)
  • Re: "C vs java"
    ... The first obvious error is the confusion of implementations with the ... language proper, in the "compilation" row. ... The "array declarations" row doesn't show how to declare an array. ... realloc but also static declaration syntax. ...
    (comp.lang.c)
  • Re: beginning with ML
    ... If you want to start from core SML and add features, ... from any of the existing implementations, an dthere would be no point ... If you want to design a new language inspired by ...
    (comp.lang.functional)
  • Re: size of a sizeof(pointer)
    ... Few things are crystal-clear in English etymology. ... ("few C implementations provide safe pointers"), ... But the language does not *depend* on this trade-off, ...
    (comp.lang.c)
  • Re: SPITBOL or SNOBOL4 in OS X unix layer?
    ... > I am a long-time user of the old-fashioned programming language package ... > MaxSPITBOL, an implementation of the text-oriented language SPITBOL, ... > But SPITBOL has had several unix implementations over the years. ... When you download it, open a Terminal window and cd to the directory ...
    (comp.sys.mac.system)