Skip to content
This repository

wi 6195 added Enumerable#group_by #33

Merged
merged 1 commit into from almost 3 years ago

2 participants

Enrico Sada Tomas Matousek
Enrico Sada

updated rubyspec ironruby-tags-19 for Enumerable#group_by

i checked also nil (used as key of dictionary :D):
(1..7).group_by {|x| nil} => {nil => [1,2,3,4,5,6,7]}

Enrico Sada enricosada wi 6195 added Enumerable#group_by
updated rubyspec ironruby-tags-19 for Enumerable#group_by
754ad39
Tomas Matousek tmat merged commit 754ad39 into from
Tomas Matousek tmat closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Aug 08, 2011
Enrico Sada enricosada wi 6195 added Enumerable#group_by
updated rubyspec ironruby-tags-19 for Enumerable#group_by
754ad39
This page is out of date. Refresh to see the latest.
36 Languages/Ruby/Libraries/Builtins/Enumerable.cs
@@ -1060,5 +1060,41 @@ public static class Enumerable {
1060 1060 // reverse_each
1061 1061 // flat_map
1062 1062 // join
  1063 +
  1064 + [RubyMethod("group_by")]
  1065 + public static Enumerator/*!*/ GetGroupByEnumerator(CallSiteStorage<EachSite>/*!*/ each, BlockParam/*!*/ predicate, object self)
  1066 + {
  1067 + return new Enumerator((_, block) => (object) GroupBy(each, block, self));
  1068 + }
  1069 +
  1070 + [RubyMethod("group_by")]
  1071 + public static Hash GroupBy(CallSiteStorage<EachSite>/*!*/ each, [NotNull]BlockParam/*!*/ predicate, object self)
  1072 + {
  1073 + var grouped = new Dictionary<object,object>();
  1074 +
  1075 + Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item)
  1076 + {
  1077 + object blockResult;
  1078 + if (predicate.Yield(item, out blockResult))
  1079 + {
  1080 + return selfBlock.PropagateFlow(predicate, blockResult);
  1081 + }
  1082 +
  1083 + if (IDictionaryOps.HasKey(grouped, blockResult))
  1084 + {
  1085 + var a = (RubyArray)IDictionaryOps.GetElement(each.Context, grouped, blockResult);
  1086 + a.Add(item);
  1087 + }
  1088 + else
  1089 + {
  1090 + IDictionaryOps.SetElement(each.Context, grouped, blockResult, new RubyArray {item});
  1091 + }
  1092 +
  1093 + return null;
  1094 + }));
  1095 +
  1096 + return new Hash(grouped);
  1097 + }
  1098 +
1063 1099 }
1064 1100 }
42 Languages/Ruby/Libraries/Initializers.Generated.cs
@@ -1229,6 +1229,12 @@ public sealed class BuiltinsLibraryInitializer : IronRuby.Builtins.LibraryInitia
1229 1229 new Func<IronRuby.Runtime.CallSiteStorage<Func<System.Runtime.CompilerServices.CallSite, System.Object, IronRuby.Builtins.Proc, System.Object>>, IronRuby.Runtime.BinaryOpStorage, IronRuby.Runtime.BlockParam, System.Object, System.Object, System.Object>(IronRuby.Builtins.Enumerable.Grep)
1230 1230 );
1231 1231
  1232 + DefineLibraryMethod(module, "group_by", 0x51,
  1233 + 0x00000000U, 0x00000002U,
  1234 + new Func<IronRuby.Runtime.CallSiteStorage<Func<System.Runtime.CompilerServices.CallSite, System.Object, IronRuby.Builtins.Proc, System.Object>>, IronRuby.Runtime.BlockParam, System.Object, IronRuby.Builtins.Enumerator>(IronRuby.Builtins.Enumerable.GetGroupByEnumerator),
  1235 + new Func<IronRuby.Runtime.CallSiteStorage<Func<System.Runtime.CompilerServices.CallSite, System.Object, IronRuby.Builtins.Proc, System.Object>>, IronRuby.Runtime.BlockParam, System.Object, IronRuby.Builtins.Hash>(IronRuby.Builtins.Enumerable.GroupBy)
  1236 + );
  1237 +
1232 1238 DefineLibraryMethod(module, "include?", 0x51,
1233 1239 0x00000000U,
1234 1240 new Func<IronRuby.Runtime.CallSiteStorage<Func<System.Runtime.CompilerServices.CallSite, System.Object, IronRuby.Builtins.Proc, System.Object>>, IronRuby.Runtime.BinaryOpStorage, System.Object, System.Object, System.Object>(IronRuby.Builtins.Enumerable.Contains)
@@ -2910,11 +2916,11 @@ public sealed class BuiltinsLibraryInitializer : IronRuby.Builtins.LibraryInitia
2910 2916 );
2911 2917
2912 2918 DefineLibraryMethod(module, "readlines", 0x51,
2913   - 0x00000000U, 0x00000000U, 0x00020004U, 0x00060000U,
  2919 + 0x00060000U, 0x00000000U, 0x00000000U, 0x00020004U,
  2920 + new Func<IronRuby.Runtime.RubyContext, IronRuby.Builtins.RubyIO, IronRuby.Builtins.MutableString, System.Int32, IronRuby.Builtins.RubyArray>(IronRuby.Builtins.RubyIOOps.ReadLines),
2914 2921 new Func<IronRuby.Runtime.RubyContext, IronRuby.Builtins.RubyIO, IronRuby.Builtins.RubyArray>(IronRuby.Builtins.RubyIOOps.ReadLines),
2915 2922 new Func<IronRuby.Runtime.RubyContext, IronRuby.Builtins.RubyIO, Microsoft.Scripting.Runtime.DynamicNull, IronRuby.Builtins.RubyArray>(IronRuby.Builtins.RubyIOOps.ReadLines),
2916   - new Func<IronRuby.Runtime.RubyContext, IronRuby.Builtins.RubyIO, IronRuby.Runtime.Union<IronRuby.Builtins.MutableString, System.Int32>, IronRuby.Builtins.RubyArray>(IronRuby.Builtins.RubyIOOps.ReadLines),
2917   - new Func<IronRuby.Runtime.RubyContext, IronRuby.Builtins.RubyIO, IronRuby.Builtins.MutableString, System.Int32, IronRuby.Builtins.RubyArray>(IronRuby.Builtins.RubyIOOps.ReadLines)
  2923 + new Func<IronRuby.Runtime.RubyContext, IronRuby.Builtins.RubyIO, IronRuby.Runtime.Union<IronRuby.Builtins.MutableString, System.Int32>, IronRuby.Builtins.RubyArray>(IronRuby.Builtins.RubyIOOps.ReadLines)
2918 2924 );
2919 2925
2920 2926 DefineLibraryMethod(module, "reopen", 0x51,
@@ -4126,15 +4132,15 @@ public sealed class BuiltinsLibraryInitializer : IronRuby.Builtins.LibraryInitia
4126 4132 );
4127 4133
4128 4134 DefineLibraryMethod(module, "define_singleton_method", 0x51,
4129   - new[] { 0x0002000cU, 0x0000000cU, 0x0002000cU, 0x0000000cU, 0x0004000aU, 0x0000000aU, 0x0002000cU, 0x0000000cU},
  4135 + new[] { 0x0000000cU, 0x0002000cU, 0x0000000cU, 0x0002000cU, 0x0000000cU, 0x0004000aU, 0x0000000aU, 0x0002000cU},
  4136 + new Func<IronRuby.Runtime.RubyScope, System.Object, IronRuby.Runtime.ClrName, IronRuby.Builtins.Proc, IronRuby.Builtins.Proc>(IronRuby.Builtins.KernelOps.DefineSingletonMethod),
4130 4137 new Func<IronRuby.Runtime.RubyScope, System.Object, System.String, IronRuby.Builtins.RubyMethod, IronRuby.Builtins.RubyMethod>(IronRuby.Builtins.KernelOps.DefineSingletonMethod),
4131 4138 new Func<IronRuby.Runtime.RubyScope, System.Object, IronRuby.Runtime.ClrName, IronRuby.Builtins.RubyMethod, IronRuby.Builtins.RubyMethod>(IronRuby.Builtins.KernelOps.DefineSingletonMethod),
4132 4139 new Func<IronRuby.Runtime.RubyScope, System.Object, System.String, IronRuby.Builtins.UnboundMethod, IronRuby.Builtins.UnboundMethod>(IronRuby.Builtins.KernelOps.DefineSingletonMethod),
4133 4140 new Func<IronRuby.Runtime.RubyScope, System.Object, IronRuby.Runtime.ClrName, IronRuby.Builtins.UnboundMethod, IronRuby.Builtins.UnboundMethod>(IronRuby.Builtins.KernelOps.DefineSingletonMethod),
4134 4141 new Func<IronRuby.Runtime.RubyScope, IronRuby.Runtime.BlockParam, System.Object, System.String, IronRuby.Builtins.Proc>(IronRuby.Builtins.KernelOps.DefineSingletonMethod),
4135 4142 new Func<IronRuby.Runtime.RubyScope, IronRuby.Runtime.BlockParam, System.Object, IronRuby.Runtime.ClrName, IronRuby.Builtins.Proc>(IronRuby.Builtins.KernelOps.DefineSingletonMethod),
4136   - new Func<IronRuby.Runtime.RubyScope, System.Object, System.String, IronRuby.Builtins.Proc, IronRuby.Builtins.Proc>(IronRuby.Builtins.KernelOps.DefineSingletonMethod),
4137   - new Func<IronRuby.Runtime.RubyScope, System.Object, IronRuby.Runtime.ClrName, IronRuby.Builtins.Proc, IronRuby.Builtins.Proc>(IronRuby.Builtins.KernelOps.DefineSingletonMethod)
  4143 + new Func<IronRuby.Runtime.RubyScope, System.Object, System.String, IronRuby.Builtins.Proc, IronRuby.Builtins.Proc>(IronRuby.Builtins.KernelOps.DefineSingletonMethod)
4138 4144 );
4139 4145
4140 4146 DefineLibraryMethod(module, "display", 0x51,
@@ -6697,11 +6703,11 @@ public sealed class BuiltinsLibraryInitializer : IronRuby.Builtins.LibraryInitia
6697 6703 );
6698 6704
6699 6705 DefineLibraryMethod(module, "each_line", 0x51,
6700   - 0x00000000U, 0x00000002U, 0x00010000U, 0x00020001U,
6701   - new Func<IronRuby.Runtime.RubyContext, IronRuby.Builtins.MutableString, IronRuby.Builtins.Enumerator>(IronRuby.Builtins.MutableStringOps.EachLine),
  6706 + 0x00000002U, 0x00010000U, 0x00020001U, 0x00000000U,
6702 6707 new Func<IronRuby.Runtime.RubyContext, IronRuby.Runtime.BlockParam, IronRuby.Builtins.MutableString, System.Object>(IronRuby.Builtins.MutableStringOps.EachLine),
6703 6708 new Func<IronRuby.Builtins.MutableString, IronRuby.Builtins.MutableString, IronRuby.Builtins.Enumerator>(IronRuby.Builtins.MutableStringOps.EachLine),
6704   - new Func<IronRuby.Runtime.BlockParam, IronRuby.Builtins.MutableString, IronRuby.Builtins.MutableString, System.Object>(IronRuby.Builtins.MutableStringOps.EachLine)
  6709 + new Func<IronRuby.Runtime.BlockParam, IronRuby.Builtins.MutableString, IronRuby.Builtins.MutableString, System.Object>(IronRuby.Builtins.MutableStringOps.EachLine),
  6710 + new Func<IronRuby.Runtime.RubyContext, IronRuby.Builtins.MutableString, IronRuby.Builtins.Enumerator>(IronRuby.Builtins.MutableStringOps.EachLine)
6705 6711 );
6706 6712
6707 6713 DefineLibraryMethod(module, "empty?", 0x51,
@@ -6812,11 +6818,11 @@ public sealed class BuiltinsLibraryInitializer : IronRuby.Builtins.LibraryInitia
6812 6818 );
6813 6819
6814 6820 DefineLibraryMethod(module, "lines", 0x51,
6815   - 0x00000000U, 0x00000002U, 0x00010000U, 0x00020001U,
6816   - new Func<IronRuby.Runtime.RubyContext, IronRuby.Builtins.MutableString, IronRuby.Builtins.Enumerator>(IronRuby.Builtins.MutableStringOps.EachLine),
  6821 + 0x00000002U, 0x00010000U, 0x00020001U, 0x00000000U,
6817 6822 new Func<IronRuby.Runtime.RubyContext, IronRuby.Runtime.BlockParam, IronRuby.Builtins.MutableString, System.Object>(IronRuby.Builtins.MutableStringOps.EachLine),
6818 6823 new Func<IronRuby.Builtins.MutableString, IronRuby.Builtins.MutableString, IronRuby.Builtins.Enumerator>(IronRuby.Builtins.MutableStringOps.EachLine),
6819   - new Func<IronRuby.Runtime.BlockParam, IronRuby.Builtins.MutableString, IronRuby.Builtins.MutableString, System.Object>(IronRuby.Builtins.MutableStringOps.EachLine)
  6824 + new Func<IronRuby.Runtime.BlockParam, IronRuby.Builtins.MutableString, IronRuby.Builtins.MutableString, System.Object>(IronRuby.Builtins.MutableStringOps.EachLine),
  6825 + new Func<IronRuby.Runtime.RubyContext, IronRuby.Builtins.MutableString, IronRuby.Builtins.Enumerator>(IronRuby.Builtins.MutableStringOps.EachLine)
6820 6826 );
6821 6827
6822 6828 DefineLibraryMethod(module, "ljust", 0x51,
@@ -6877,10 +6883,10 @@ public sealed class BuiltinsLibraryInitializer : IronRuby.Builtins.LibraryInitia
6877 6883 );
6878 6884
6879 6885 DefineLibraryMethod(module, "rindex", 0x51,
6880   - 0x00040004U, 0x00010002U, 0x00030002U,
6881   - new Func<IronRuby.Runtime.RubyScope, IronRuby.Builtins.MutableString, IronRuby.Builtins.RubyRegex, System.Int32, System.Object>(IronRuby.Builtins.MutableStringOps.LastIndexOf),
  6886 + 0x00010002U, 0x00030002U, 0x00040004U,
6882 6887 new Func<IronRuby.Builtins.MutableString, IronRuby.Builtins.MutableString, System.Object>(IronRuby.Builtins.MutableStringOps.LastIndexOf),
6883   - new Func<IronRuby.Builtins.MutableString, IronRuby.Builtins.MutableString, System.Int32, System.Object>(IronRuby.Builtins.MutableStringOps.LastIndexOf)
  6888 + new Func<IronRuby.Builtins.MutableString, IronRuby.Builtins.MutableString, System.Int32, System.Object>(IronRuby.Builtins.MutableStringOps.LastIndexOf),
  6889 + new Func<IronRuby.Runtime.RubyScope, IronRuby.Builtins.MutableString, IronRuby.Builtins.RubyRegex, System.Int32, System.Object>(IronRuby.Builtins.MutableStringOps.LastIndexOf)
6884 6890 );
6885 6891
6886 6892 DefineLibraryMethod(module, "rjust", 0x51,
@@ -11232,12 +11238,12 @@ public sealed class BigDecimalLibraryInitializer : IronRuby.Builtins.LibraryInit
11232 11238 );
11233 11239
11234 11240 DefineLibraryMethod(module, "<=>", 0x11,
11235   - new[] { 0x00000002U, 0x00000004U, 0x00000000U, 0x00000000U, 0x00000000U},
  11241 + new[] { 0x00000000U, 0x00000002U, 0x00000004U, 0x00000000U, 0x00000000U},
  11242 + new Func<IronRuby.Runtime.BinaryOpStorage, IronRuby.Runtime.BinaryOpStorage, IronRuby.StandardLibrary.BigDecimal.BigDecimal, System.Object, System.Object>(IronRuby.StandardLibrary.BigDecimal.BigDecimalOps.Compare),
11236 11243 new Func<IronRuby.StandardLibrary.BigDecimal.BigDecimal, IronRuby.StandardLibrary.BigDecimal.BigDecimal, System.Object>(IronRuby.StandardLibrary.BigDecimal.BigDecimalOps.Compare),
11237 11244 new Func<IronRuby.Runtime.RubyContext, IronRuby.StandardLibrary.BigDecimal.BigDecimal, Microsoft.Scripting.Math.BigInteger, System.Object>(IronRuby.StandardLibrary.BigDecimal.BigDecimalOps.Compare),
11238 11245 new Func<IronRuby.Runtime.RubyContext, IronRuby.StandardLibrary.BigDecimal.BigDecimal, System.Int32, System.Object>(IronRuby.StandardLibrary.BigDecimal.BigDecimalOps.Compare),
11239   - new Func<IronRuby.Runtime.RubyContext, IronRuby.StandardLibrary.BigDecimal.BigDecimal, System.Double, System.Object>(IronRuby.StandardLibrary.BigDecimal.BigDecimalOps.Compare),
11240   - new Func<IronRuby.Runtime.BinaryOpStorage, IronRuby.Runtime.BinaryOpStorage, IronRuby.StandardLibrary.BigDecimal.BigDecimal, System.Object, System.Object>(IronRuby.StandardLibrary.BigDecimal.BigDecimalOps.Compare)
  11246 + new Func<IronRuby.Runtime.RubyContext, IronRuby.StandardLibrary.BigDecimal.BigDecimal, System.Double, System.Object>(IronRuby.StandardLibrary.BigDecimal.BigDecimalOps.Compare)
11241 11247 );
11242 11248
11243 11249 DefineLibraryMethod(module, "==", 0x11,
3  Languages/Ruby/Tests/mspec/ironruby-tags-19/core/enumerable/group_by_tags.txt
... ... @@ -1,3 +0,0 @@
1   -fails:Enumerable#group_by returns a hash with values grouped according to the block
2   -fails:Enumerable#group_by returns an empty hash for empty enumerables
3   -fails:Enumerable#group_by returns an Enumerator if called without a block

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.