Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement Array#sample and Arran#sample(n) as per rubyspec

  • Loading branch information...
commit f041d202bb4c64de0ac6839fa81848a552af103b 1 parent 7ded4c3
Orion Edwards [GGL] authored
View
41 Languages/Ruby/Libraries/Extensions/IListOps.cs
@@ -1882,7 +1882,7 @@ public static class IListOps {
#endregion
- #region shuffle, shuffle!
+ #region shuffle, shuffle!, sample
[RubyMethod("shuffle")]
public static IList/*!*/ Shuffle(UnaryOpStorage/*!*/ allocateStorage, RubyArray/*!*/ self) {
@@ -1921,6 +1921,45 @@ public static class IListOps {
return self;
}
+ [RubyMethod("sample")]
+ public static object Sample(RubyContext/*!*/ context, IList/*!*/ self) {
+ var generator = context.RandomNumberGenerator;
+ if (self.Count == 0) {
+ return null;
+ }
+ return self[generator.Next(self.Count)];
+ }
+
+ [RubyMethod("sample")]
+ public static IList/*!*/ Sample(UnaryOpStorage/*!*/ allocateStorage, ConversionStorage<int>/*!*/ fixnumCast, IList/*!*/ self, object elementCount) {
+ return Sample(allocateStorage, self, Protocols.CastToFixnum(fixnumCast, elementCount));
+ }
+
+ [RubyMethod("sample")]
+ public static IList/*!*/ Sample(UnaryOpStorage/*!*/ allocateStorage, IList/*!*/ self, [DefaultProtocol]int elementCount) {
+ if (elementCount < 0) {
+ throw RubyExceptions.CreateArgumentError("negative sample number");
+ }
+
+ IList result = CreateResultArray(allocateStorage, self);
+ if (self.Count == 0) {
+ return result;
+ }
+
+ var generator = allocateStorage.Context.RandomNumberGenerator;
+
+ // we can't pick the same element twice, so remove items from a copy of the list as we pick them
+ var itemsRemaining = new ArrayList(self);
+
+ while (itemsRemaining.Count > 0 && elementCount--> 0) {
+ var idx = generator.Next(itemsRemaining.Count);
+ result.Add(itemsRemaining[idx]);
+ itemsRemaining.RemoveAt(idx);
+ }
+
+ return result;
+ }
+
#endregion
#region reverse, reverse!, transpose, uniq, uniq!
View
7 Languages/Ruby/Libraries/Initializers.Generated.cs
@@ -8140,6 +8140,13 @@ public sealed class BuiltinsLibraryInitializer : IronRuby.Builtins.LibraryInitia
new Func<IronRuby.Runtime.BinaryOpStorage, IronRuby.Runtime.BlockParam, System.Collections.IList, System.Object, System.Object>(IronRuby.Builtins.IListOps.ReverseIndex)
);
+ DefineLibraryMethod(module, "sample", 0x51,
+ 0x00000000U, 0x00000000U, 0x00020000U,
+ new Func<IronRuby.Runtime.RubyContext, System.Collections.IList, System.Object>(IronRuby.Builtins.IListOps.Sample),
+ new Func<IronRuby.Runtime.UnaryOpStorage, IronRuby.Runtime.ConversionStorage<System.Int32>, System.Collections.IList, System.Object, System.Collections.IList>(IronRuby.Builtins.IListOps.Sample),
+ new Func<IronRuby.Runtime.UnaryOpStorage, System.Collections.IList, System.Int32, System.Collections.IList>(IronRuby.Builtins.IListOps.Sample)
+ );
+
DefineLibraryMethod(module, "select!", 0x51,
0x00000000U,
new Func<IronRuby.Runtime.BlockParam, System.Collections.IList, System.Object>(IronRuby.Builtins.IListOps.SelectInPlace)
Please sign in to comment.
Something went wrong with that request. Please try again.