Subscribe: How you can have Ruby-style enumerations in C# 3.0
http://www.tkachenko.com/blog/archives/commentrss/000683.xml
Added By: Feedage Forager Feedage Grade B rated
Language: English
Tags:
action  comment  console writeline  declare generic  delegate  generic parameter  generic  public  ruby  squares  val  void  writeline val 
Rate this Feed
Rate this feedRate this feedRate this feedRate this feedRate this feed
Rate this feed 1 starRate this feed 2 starRate this feed 3 starRate this feed 4 starRate this feed 5 star

Comments (0)

Feed Details and Statistics Feed Statistics
Preview: How you can have Ruby-style enumerations in C# 3.0

How you can have Ruby-style enumerations in C# 3.0



Well, I missed MVP Summit this year, so while fellow MVPs enjoying together in Redmond I'm playing with C# 3.0 at home. And I'm in the process of Ruby learning, so what I spotted immediately is the lack (correct me if I'm wrong) of Each() and Map() suppor



Published: 2007-03-16T01:33:37-08:00

 



Comment by Aaron: Instead of: squares.Each(val => Console.WriteLine(val...

2007-03-20T17:28:10-08:00

Instead of:
squares.Each(val => Console.WriteLine(val));

Why not just do
foreach (var val in squares) Console.WriteLine(val);

Pretty simple, faster, and no funky extension methods to worry about (+ you dont have to write another using *** at the top of every class file). Guess its not as cool though ;)

Also, not to nitpick, but:
"""Each() method is basically a Visitor pattern implementation. """

Thinking about "Each" as any kind of pattern waaaaay over complicates it. Its just iteration over elements. :)




Comment by Oleg Tkachenko: Thanks guys for mentioning Action...

2007-03-18T13:20:40-08:00

Thanks guys for mentioning Action delegate, I'm confirmed total ignoramus. I updated the code.

Scott, I think I have to declare generic parameter in method declaration - first AFAIK there is no non-generic Action version and then it would require working with object and so downcasting in lambda function. I'm not sure I'm following you though.

Btw, I omitted generic parameter in usage code - it could be written as

squares.EachIndex(i => squares[i] = i * i);

but I wanted to be it as close to Ruby as it can be.

Mark, IndexedAction is also good idea, that matches Ruby's Enumerable.each_with_index() method.




Comment by Mark: (woking in .Neet 2.0) The...

2007-03-16T22:30:07-08:00

(woking in .Neet 2.0)

The List class has a ForEach function :-

public void ForEach(Action action)

, which is not as flexible as your extension methods, but you may wish to utilise the Action delegate from the System namespace:-

public delegate void Action(T obj);

Having said that I then created another delegate:-

public delegate bool IndexedAction(T item, int position);

to pass the index and the object.




Comment by Scott Dukes: Hi Oleg [1] Rather than...

2007-03-16T17:21:58-08:00

Hi Oleg

[1] Rather than define a new delegate why not simply use the existing Action?

[2] Surely you have to declare your generic parameter?

eg.
public static void Each(this IEnumerable source, Action sub), and
public static void EachIndex(this IEnumerable source, Action sub)

Nit-picking aside; that is a slick solution. :-)