Converting Ruby "each" iterator to C#

D

dkmd_nielsen

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top