Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2007

Tip: Looking for answers? Try searching our database.

Converting Ruby "each" iterator to C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dkmd_nielsen - 18 May 2007 14:37 GMT
I have two rather simple class methods coded in Ruby...my own each
iterator:  The iterator is used internally within the class/namespace,
and be available externally.  That way I can keep everything hidden
about the "instructions table."

# Loop through each instruction in the block, yielding the result from
  # the specified code block.
  def each(&logic)
     @instructions.each {|instr| yield instr}
  end

# Find the specified instruction parm (string) in the block.  Returns
# nil if parm not found.
  def find(p)
     self.each {|i| return i if i.parm.index(p) }
     nil
  end

I'm trying to recode this exact functionality in C#, and am just not
getting the Delegate stuff.  Can you direct me to some online examples
or references that show how to pass a delegate into method that
implements an iterator?  I'm just not getting all the new C#
terminology and syntax complexities, and I need to learn it.  MSDN
isn't helping me at all.  The following is quasi psuedo mock up of
what I'm trying to accomplish.

       public void each(Delegate block)
       {
           foreach (FL_Instruction i in this.Instructions)
           {
               block;
           }
       }

       public static FL_Instruction find(string parm)
       {
           this.each( delegate(FL_Instruction i) { if
(i.Parm.IndexOf(parm) > 0) { return i; } };);
           return null;
       }

Thanks for your time and consideration.  Once I can get that "Ah
Haaa...now I got it" feeling, I'll be good to go for the future.
dvn
Ignacio Machin ( .NET/ C# MVP ) - 18 May 2007 15:18 GMT
Hi,

Why you need delegates to create an iterator?

See the doc for IEnumerator

Must of the time you dont have to implement it, just inherit from
CollectionBase (or use List<T> if you are using 2.0) and you get it for
free.

>I have two rather simple class methods coded in Ruby...my own each
> iterator:  The iterator is used internally within the class/namespace,
[quoted text clipped - 40 lines]
> Haaa...now I got it" feeling, I'll be good to go for the future.
> dvn
Nicholas Paldino [.NET/C# MVP] - 18 May 2007 17:10 GMT
dvn,

   Ok, so it looks like you want to do something which LINQ is going to
accomplish in the next release of C#.

   What it seems like is that you want to pass a conditional to the method,
and have it return an IEnumerable which will only return those elements.
You can do this in .NET 2.0, like so:

public static IEnumerable<T> Where<T>(IEnumerable<T> enumerable,
Predicate<T> predicate)
{
   // Cycle through the items.
   foreach (T item in enumerable)
   {
       // If the predicate returns true, then yield the item.
       if (predicate(item))
       {
           // Yield the item.
           yield return item;
       }
   }
}

   Then, you can do something like this:

// Assume array is an array of integers.
// Only return odd.
foreach (int i in Where(array, delegate(int) { return (i % 2) == 0; }))
{
   // Do something with i.
}

   The neat thing is that you can chain the calls together:

// Get the enuerable for only odd numbers.
IEnumerable<int> oddNumbers = Where(array, delegate(int) { return (i % 2) ==
0; });

// Get only odd numbers with the digit "1" in it.
IEnumerable<int> oddNumbersWithOne = Where(oddNumbers, delegate(int) {
return i.ToString().Contains("1"); });

// Cycle through the numbers now.
foreach (int i in oddNumbersWithOne)
{
   // Do something.
}

   Hope this helps.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

>I have two rather simple class methods coded in Ruby...my own each
> iterator:  The iterator is used internally within the class/namespace,
[quoted text clipped - 40 lines]
> Haaa...now I got it" feeling, I'll be good to go for the future.
> dvn

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.