'Repeat' pattern in C# for batch processing

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

    'Repeat' pattern in C# for batch processing

    Hello,

    I was wondering if there is something equivalent to 'Repeat' pattern
    in C# where I can say,

    Repeat(10) myobj.SayHi();

    The expansion of this being,

    for (int i = 0; i < 10; ++i)
    {
    myObj.SayHi();
    }

    This is like a counted loop in assembly programming ...

    Thanks,
    WinCPP
  • raylopez99

    #2
    Re: 'Repeat' pattern in C# for batch processing

    Well I think you just answered your question: use a loop.

    RL

    win_cpp wrote:
    Hello,
    >
    I was wondering if there is something equivalent to 'Repeat' pattern
    in C# where I can say,
    >
    Repeat(10) myobj.SayHi();
    >
    The expansion of this being,
    >
    for (int i = 0; i < 10; ++i)
    {
    myObj.SayHi();
    }
    >
    This is like a counted loop in assembly programming ...
    >
    Thanks,
    WinCPP

    Comment

    • Paul Musso

      #3
      Re: 'Repeat' pattern in C# for batch processing

      Dans son message précédent, win_cpp a écrit :
      Hello,
      >
      I was wondering if there is something equivalent to 'Repeat' pattern
      in C# where I can say,
      >
      Repeat(10) myobj.SayHi();
      >
      The expansion of this being,
      >
      for (int i = 0; i < 10; ++i)
      {
      myObj.SayHi();
      }
      >
      This is like a counted loop in assembly programming ...
      >
      Thanks,
      WinCPP
      Hi WinCPP,

      C# doesn't include this type of operator.
      But you can create a utility method like that :

      public static void Repeat(int number, Action action)
      {
      for (int i = 0; i < number; i++)
      {
      action();
      }
      }

      And, when you are using it, it makes :

      Repeat(10,() ={ myObj.SayHi(); });

      --
      Paul Musso


      Comment

      • Duggi

        #4
        Re: 'Repeat' pattern in C# for batch processing

        On Sep 22, 8:10 am, Paul Musso <pa...@exakis.c omwrote:
        Dans son message précédent, win_cpp a écrit :
        >
        >
        >
        Hello,
        >
        I was wondering if there is something equivalent to 'Repeat' pattern
        in C# where I can say,
        >
        Repeat(10) myobj.SayHi();
        >
        The expansion of this being,
        >
        for (int i = 0; i < 10; ++i)
        {
            myObj.SayHi();
        }
        >
        This is like a counted loop in assembly programming ...
        >
        Thanks,
        WinCPP
        >
        Hi WinCPP,
        >
        C# doesn't include this type of operator.
        But you can create a utility method like that :
        >
        public static void Repeat(int number, Action action)
        {
            for (int i = 0; i < number; i++)
            {
                action();
            }
        >
        }
        >
        And, when you are using it, it makes :
        >
        Repeat(10,() ={ myObj.SayHi(); });
        >
        --
        Paul Musso
        thats a good abstraction... to make Repeat()

        .....

        -Cnu

        Comment

        • win_cpp

          #5
          Re: 'Repeat' pattern in C# for batch processing

          Hi Paul,

          Thanks for the suggestion. I try writing that.

          As for using for loop, I was a bit lazy to type all the for (; ;)
          stuff just to make a function call for 'known' number of times. So I
          was looking for syntactic sugar which can reduce the burden of typing
          and thus making the code less error prone.

          At the language level, I was wondering if such an inbuilt
          functionality would help in case of the loops for which the count is
          known. As is evident from the usage, the 'Action' would be independent
          of the induction variable for the loop. Compilers typically do
          induction variable analysis and then perform loop inversions if it can
          be established that the code *will* execute at least once. If there
          were a construct such as 'Repeat' I guess it would consierably help
          optimizations.

          Regards,
          WinCPP


          But you can create a utility method like that :
          >
          public static voidRepeat(int number, Action action)
          {
              for (int i = 0; i < number; i++)
              {
                  action();
              }
          >
          }
          >
          And, when you are using it, it makes :
          >
          Repeat(10,() ={ myObj.SayHi(); });
          >

          Comment

          • JS

            #6
            Re: 'Repeat' pattern in C# for batch processing

            As for using for loop, I was a bit lazy to type all the for (; ;)
            stuff just to make a function call for 'known' number of times. So I
            was looking for syntactic sugar which can reduce the burden of typing
            and thus making the code less error prone.
            Others who read your code will be very familiar with the for(;;)
            statement, but it might confuse them to see some special looping
            construct. In my opinion, you should stick with a for loop for
            readability. Don't succumb to laziness at the expense of readability.

            Comment

            Working...