473,796 Members | 2,801 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "instructio ns table."

# Loop through each instruction in the block, yielding the result from
# the specified code block.
def each(&logic)
@instructions.e ach {|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.Instructio ns)
{
block;
}
}

public static FL_Instruction find(string parm)
{
this.each( delegate(FL_Ins truction 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 2859
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_niels en" <do**@cmscms.co mwrote in message
news:11******** *************@u 30g2000hsc.goog legroups.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 "instructio ns table."

# Loop through each instruction in the block, yielding the result from
# the specified code block.
def each(&logic)
@instructions.e ach {|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.Instructio ns)
{
block;
}
}

public static FL_Instruction find(string parm)
{
this.each( delegate(FL_Ins truction 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<TWh ere<T>(IEnumera ble<Tenumerable ,
Predicate<Tpred icate)
{
// 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 oddNumbersWithO ne = Where(oddNumber s, delegate(int) {
return i.ToString().Co ntains("1"); });

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

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

"dkmd_niels en" <do**@cmscms.co mwrote in message
news:11******** *************@u 30g2000hsc.goog legroups.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 "instructio ns table."

# Loop through each instruction in the block, yielding the result from
# the specified code block.
def each(&logic)
@instructions.e ach {|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.Instructio ns)
{
block;
}
}

public static FL_Instruction find(string parm)
{
this.each( delegate(FL_Ins truction 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2179
by: Rick | last post by:
Hello all. I have an index.php file that has a lot of functions that I wrote. Let's say for the sake of argument that there are 1000 functions of 100 lines each. The index.php file is invoked with a "state" a la "index.php?state=L1-2L3C4-47". Different functions are called according to what the state is, and then user actions can cause index.php to be invoked again with a different state. For instance, you can click an "erase this...
5
5002
by: Orin Hargraves | last post by:
I'm formatting data in a text file using a "For Each aPara" statement along with a "Next aPara." How do I make the thing stop when it reaches the end of the file, rather than starting again at the top of the file and looping forever? Thanks, I'm new at this. Orin Hargraves
3
1783
by: Robin Tucker | last post by:
Hi, Can anyone tell me how to select the "most recent" date values from a grouped query? Consider the following: CREATE TABLE . ( NOT NULL , NOT NULL , NOT NULL ) ON This is a simplified adjacency list. What I want to do is find the highest
3
2126
by: Phil | last post by:
Hi everybody, I am a XSLT beginner and the following problem really makes me crazy ! I have a main "contacts.xml" document which contains references to several contact data XML files. My aim is to process the contacts in a single-pass XSLT process. That is why the "document()" function is what I need. I call the "document()" XPath function from a "for-each" instruction.
17
4726
by: John Grandy | last post by:
What is the C# equivalent to the VB.Net looping structure: Protected rbl As RadioButtonList Dim li As ListItem For Each li In rbl.Items Next li
7
1318
by: aska | last post by:
Hi. I heard that vs2005 vc++ support the "for each" statement. I try it in vs2005 by creating a dotnet c++ console program. The code is: #using <System.dll> using namespace System; using namespace stdcli::language; void main(){ array<int>^ ary1 = gcnew array<int>(4){1,2,3,4};
2
1412
by: Dexter | last post by:
Hello all, I have webform1, and i need to make a bow to catch all textbox in my webform1. I tray this, but don't function. Dim obj As System.Web.UI.WebControls.TextBox Dim cont As Integer For Each obj In Page.Controls. If TypeOf obj Is System.Web.UI.WebControls.TextBox Then obj.Text = "It is a Textbox"
3
5637
by: Wang Xiaoning | last post by:
i put order-by here, but i got the invalid attribut error, what's missing? thanks ----------------------------------------------------------------------------------------------------------- <?xml version="1.0" encoding="utf-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
3
1815
by: Dale | last post by:
Access 2000 I am trying to check the form to be sure that required fields are entered. For each required field (Control) I have set the tag property to "1". I am trying to loop through all controls with a tag property of "1", If they are null, I want to stop the loop and setfocus on the control. The below code works Except it will not stop looping when a control is null. It will display the msgBox and then continue to the next...
0
10449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10168
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.