Re: foreach enhancement

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

From: cody (please_dont.spam.deutronium_at_gmx.de)
Date: 07/16/04


Date: Fri, 16 Jul 2004 03:05:07 +0200


> > > Ahh, I thought the point was to be able to create sequences that would
> > allow
> > > you to iterate over all members of the sequence/range, or multiple
> > > sequences/ranges, using foreach.
> >
> > Indeed, that was the point.
> >
> > > I didn't realize the only acceptable option was a language re-design.
I
> > > suppose I don't see the benefit of changing the language syntax in
order
> > to
> > > accomplish something that can be done in half an hour.
> >
> > You'll agree, that foreach (a in 1..100, 200..300) is far better than
> >
> > foreach (a in SequenceBuilder.GetSequence(1, 100) +
> > SequenceBuilder.GetSequence(200, 300))
> >
> > don't you? And exactly that is the purpose of my proposal.
> > Loops are one of the most common used things and making them more
readable
> > and maintainable should be a desirable goal, don't you agree?
>
> 'Better' is a subjective term; and I actually don't agree that it's
> 'better'. BTW, I'm sure you haven't noticed, but I've been with this
thread
> from the beginning. In fact, I was the first poster to defend your
general
> ideas about improving the quality of life for programmers after your ideas
> were initially blasted by other posters.

Maybe you've read the thread but you seem not to understand the problem.

> I have a sequence 1..100. Is it possible to create a dynamically
generated
> sequence at run-time using your method like this:
>
> int x = 1;
> int y = 100;
> foreach (a in x..y)

Sure. Why should that be a problem.

> Now, assuming I want to specifically exclude 10..20 or 30..40 based on a
> user response at run-time, how do I accomplish this using ..?

Nobody was talking here about excluding things from a range. It it not
possible
and I do not see very much benefit in it. You can declare a fixed number of
ranges
that might overlap and the arguments are evaluated at runtime.
Additionally we were talking about stating increment values for cases like
datetime
which cannot be incremented simply by ++. Extra syntax would be required and
I don't think it is nessecary to introducing something similar to the very
funny
syntax you just suggested.

> > foreach (a in SequenceBuilder.GetSequence(1, 100) +
> > SequenceBuilder.GetSequence(200, 300))
>
> I would say that
>
> foreach (a in Seq(1,100) + Seq(200, 300))

What is Seq? If it is a static method it has to be declared in the current
class to use it like that, otherwise you have to state the class name in
front of the method. If the method is nonstatic you have to provide and
object, you know.
One possibility would be to use a ctor which would then look like this
(provided that the class Seq's namespace is imported):

foreach (a in new Seq(1,100) + new Seq(200, 300))

And again, nobody would create a new class and use it everywhere in his
project only to make its loops look better and perform much slower. Would
YOU do that? Surely not.

But if you simply could write foreach (int a in 100..0) wouldn't you used
that instead of for (int i=100; i>=0; i--)? Sure you would.

compare this:

// 3 accesses to the variable in the loop head are redundant and lead to
mistakes:
// It often happens to me that I mix i up with j and similar or
inadvertently use ++ instead of --.
for (int i=w; i>=0; i--)
  for (int j=h; j>=0; j--)
 {
   Foo(i * j);
 }

with that:

foreach (i in w..0)
  foreach (j in h..0)
 {
   Foo(i * j);
  }

Which one is more readable?

-- 
cody
[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk


Relevant Pages

  • Re: C# very optimisation
    ... >>> Another way to make your code faster is to never use foreach loops. ... where the iteration is actually the bottleneck. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: foreach loops are sooooo tricky.....
    ... loops are not at all like for loops, ... Translating Fortran to PHP, because hosters won't allow anything else ... I wish PHP would do array and matrix stuff like Fortran or C, ... I find foreach loops to be quite intuitive. ...
    (comp.lang.php)
  • Re: Anything wrong with the way I use "break"s in my loops?
    ... There isn't anything logically wrong the way you use "break" twice to get out of the 2 loops, but a few lines of tedious code. ... foreach ... origWeight = bu.OriginalWeight; ...
    (microsoft.public.dotnet.general)
  • Re: foreach enhancement
    ... >> foreach ) ... > Loops are one of the most common things in programming so making them more ... deserves *no* extra syntax. ... Have you ever designed a compiler? ...
    (microsoft.public.dotnet.languages.csharp)