472,090 Members | 1,293 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,090 software developers and data experts.

Converting Ruby "each" iterator to C#

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

May 18 '07 #1
2 2767
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<Tif you are using 2.0) and you get it for
free.

"dkmd_nielsen" <do**@cmscms.comwrote in message
news:11*********************@u30g2000hsc.googlegro ups.com...
>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

May 18 '07 #2
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<TWhere<T>(IEnumerable<Tenumerable,
Predicate<Tpredicate)
{
// 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<intoddNumbers = Where(array, delegate(int) { return (i % 2) ==
0; });

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

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

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"dkmd_nielsen" <do**@cmscms.comwrote in message
news:11*********************@u30g2000hsc.googlegro ups.com...
>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

May 18 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Rick | last post: by
5 posts views Thread by Orin Hargraves | last post: by
17 posts views Thread by John Grandy | last post: by
2 posts views Thread by Dexter | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.