Saturday, June 11, 2011

Comparison of Ruby's Enumerable and C#'s Linq to Object #2

The samples for Grouping in interactive shell.

Ruby 1.9's Enumerable on Mac OS X

$ irb1.9
irb(main):001:0> one_to_ten = 1..10
=> 1..10
irb(main):002:0> one_to_ten.group_by {|i| i % 3}
=> {1=>[1, 4, 7, 10], 2=>[2, 5, 8], 0=>[3, 6, 9]}

Mono(C#)'s Linq to Object on Mac OS X

$ csharp
Mono C# Shell, type "help;" for help

Enter statements below.
csharp> using System.Linq;
csharp> int[] oneToTen = new int[]{1,2,3,4,5,6,7,8,9,10};
csharp> oneToTen.GroupBy(i => i % 3).ToDictionary(g => g.Key, g => g.ToList());
{{ 1, { 1, 4, 7, 10 } }, { 2, { 2, 5, 8 } }, { 0, { 3, 6, 9 } }}

No comments:

Post a Comment