New foreach syntax?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Niall

    New foreach syntax?

    Ok, maybe this is getting too lazy and too demanding, but hey, I thought I'd
    see what people thought about it.

    Would anyone else find a syntax like:

    foreach (string Name in CompanyNames and EmployeeNames and ...)
    {
    etc
    }

    useful? I know that on some collections, you can append, insert etc. But
    it's not always the case that you have such collections when you are
    enumerating. Also, often you do not want to change one of the original
    collections by appending the elements from the other collection.

    So you are left with two choices. Make a new collection and stuff in all the
    combined elements you want, which will be a performance hit. Or write the
    foreach loop once for each collection you want to go through, which results
    in code duplication, increasing likelihood of refactoring causing bugs. I
    think the "and" syntax would provide a simple, succinct and very readable
    alternative to this...

    Unless, of course, I'm on crack and there's some easy way to do this that
    I've overlooked?

    Niall


  • Michael Bray

    #2
    Re: New foreach syntax?

    "Niall" <asdf@me.com> wrote in
    news:Ok4EFwfjDH A.2404@TK2MSFTN GP12.phx.gbl:
    [color=blue]
    > foreach (string Name in CompanyNames and EmployeeNames and ...)
    > {
    > etc
    > }
    >[/color]
    [snip][color=blue]
    >
    > Unless, of course, I'm on crack and there's some easy way to do this
    > that I've overlooked?[/color]

    How about:

    IEnumerable[] ies = new IEnumerable[] { CompanyNames, EmployeeNames };
    for(int i=0;i<ies.Lengt h; i++)
    foreach(string st in ies[i]) Console.WriteLi ne(st);

    -mbray

    Comment

    • Daniel O'Connell

      #3
      Re: New foreach syntax?


      "Niall" <asdf@me.com> wrote in message
      news:Ok4EFwfjDH A.2404@TK2MSFTN GP12.phx.gbl...[color=blue]
      > Ok, maybe this is getting too lazy and too demanding, but hey, I thought[/color]
      I'd[color=blue]
      > see what people thought about it.
      >
      > Would anyone else find a syntax like:
      >
      > foreach (string Name in CompanyNames and EmployeeNames and ...)
      > {
      > etc
      > }
      >
      > useful? I know that on some collections, you can append, insert etc. But
      > it's not always the case that you have such collections when you are
      > enumerating. Also, often you do not want to change one of the original
      > collections by appending the elements from the other collection.
      >
      > So you are left with two choices. Make a new collection and stuff in all[/color]
      the[color=blue]
      > combined elements you want, which will be a performance hit. Or write the
      > foreach loop once for each collection you want to go through, which[/color]
      results[color=blue]
      > in code duplication, increasing likelihood of refactoring causing bugs. I
      > think the "and" syntax would provide a simple, succinct and very readable
      > alternative to this...
      >
      > Unless, of course, I'm on crack and there's some easy way to do this that
      > I've overlooked?[/color]
      maybe somthing like:

      public class MultiEnumerator : IEnumerator
      {
      public MultiEnumerator (params IEnumerable[] enumerators)
      {
      IList enums = new ArrayList();
      foreach (IEnumerable enumerable in enumerators)
      {
      enums.Add(enume rable.GetEnumer ator());
      }
      }
      IEnumerator[] enumerators;
      int currentEnum=0;


      public void Reset()
      {
      // TODO: Add MultiEnumerator .Reset implementation
      currentEnum = 0;
      foreach (IEnumerator enumerator in enumerators)
      {
      enumerator.Rese t();
      }
      }

      public object Current
      {
      get
      {
      // TODO: Add MultiEnumerator .Current getter implementation
      return enumerators[currentEnum].Current;
      }
      }

      public bool MoveNext()
      {
      // TODO: Add MultiEnumerator .MoveNext implementation
      bool next = enumerators[currentEnum].MoveNext();
      if (!next)
      {
      currentEnum++;
      if (enumerators.Le ngth >= currentEnum)
      return MoveNext();
      else
      return false;
      }
      return true;
      }

      }

      Note, I didn't test this, so if I made a mistake(probabl y did) lemme know.
      Cubs game is still going and I just don't have the time to check it, ;)[color=blue]
      >
      > Niall
      >
      >[/color]


      Comment

      • Niall

        #4
        Re: New foreach syntax?

        Yeah, I've since had this solution (as well as Daniel's IEnumerator
        implementation) suggested to me by a guy at work. I don't mind this one,
        though the IEnumerator implementation is a fair bit of work. My argument was
        that foreach exists simply as a convenience over a straight for loop... and
        the "and" solution was much more convenient than the other solutions. I was
        laughed at! :P

        Niall

        "Michael Bray" <mbrayXXnoSPAMp leaseXX@SkPiAlM l.ctiusa.com> wrote in message
        news:Xns940EDED 13B022mbrayxyzc tiusacom@207.46 .248.16...[color=blue]
        > "Niall" <asdf@me.com> wrote in
        > news:Ok4EFwfjDH A.2404@TK2MSFTN GP12.phx.gbl:
        >[color=green]
        > > foreach (string Name in CompanyNames and EmployeeNames and ...)
        > > {
        > > etc
        > > }
        > >[/color]
        > [snip][color=green]
        > >
        > > Unless, of course, I'm on crack and there's some easy way to do this
        > > that I've overlooked?[/color]
        >
        > How about:
        >
        > IEnumerable[] ies = new IEnumerable[] { CompanyNames, EmployeeNames };
        > for(int i=0;i<ies.Lengt h; i++)
        > foreach(string st in ies[i]) Console.WriteLi ne(st);
        >
        > -mbray[/color]


        Comment

        • Daniel O'Connell

          #5
          Re: New foreach syntax?


          "Niall" <asdf@me.com> wrote in message
          news:ebtbQRhjDH A.684@TK2MSFTNG P09.phx.gbl...[color=blue]
          > Yeah, I've since had this solution (as well as Daniel's IEnumerator
          > implementation) suggested to me by a guy at work. I don't mind this one,
          > though the IEnumerator implementation is a fair bit of work. My argument[/color]
          was[color=blue]
          > that foreach exists simply as a convenience over a straight for loop...[/color]
          and[color=blue]
          > the "and" solution was much more convenient than the other solutions. I[/color]
          was[color=blue]
          > laughed at! :P
          >[/color]
          It really is a form of convience, but an enumerator can be a little more
          complicated than a simple for. Anyway, there are a number of things that
          have been suggested for foreach, from support for adding & removing objects
          from the collection, language level support for sortable enumerators, etc.
          The question is really are they worth it, or does the simple IEnumerator
          wrapper idea do the job with less fuss.[color=blue]
          > Niall
          >
          > "Michael Bray" <mbrayXXnoSPAMp leaseXX@SkPiAlM l.ctiusa.com> wrote in[/color]
          message[color=blue]
          > news:Xns940EDED 13B022mbrayxyzc tiusacom@207.46 .248.16...[color=green]
          > > "Niall" <asdf@me.com> wrote in
          > > news:Ok4EFwfjDH A.2404@TK2MSFTN GP12.phx.gbl:
          > >[color=darkred]
          > > > foreach (string Name in CompanyNames and EmployeeNames and ...)
          > > > {
          > > > etc
          > > > }
          > > >[/color]
          > > [snip][color=darkred]
          > > >
          > > > Unless, of course, I'm on crack and there's some easy way to do this
          > > > that I've overlooked?[/color]
          > >
          > > How about:
          > >
          > > IEnumerable[] ies = new IEnumerable[] { CompanyNames, EmployeeNames };
          > > for(int i=0;i<ies.Lengt h; i++)
          > > foreach(string st in ies[i]) Console.WriteLi ne(st);
          > >
          > > -mbray[/color]
          >
          >[/color]


          Comment

          Working...