Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don’t worry, you can still create the pull request.
  • 4 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
View
2  lib/core/facets/array.rb
@@ -36,6 +36,7 @@
require_relative 'array/pull.rb'
require_relative 'array/occurent.rb'
#require_relative 'array/op_pow.rb' # uncommon
+require_relative 'array/random_subarray.rb'
require_relative 'array/recurse.rb'
require_relative 'array/recursively.rb'
require_relative 'array/rotate.rb'
@@ -65,4 +66,3 @@
# next unless file.end_with?('.rb')
# loader.call(file)
#end
-
View
18 lib/core/facets/array/random_subarray.rb
@@ -0,0 +1,18 @@
+# Generates random subarrays. Uses random numbers and bit-
+# fiddling to assure performant uniform distributions even
+# for large arrays.
+#
+# a = *1..5
+# a.random_subset(2) #=> [[3, 4, 5], []]
+#
+# CREDIT: Michael Kohl
+
+class Array
+ def random_subarray(n=1)
+ raise ArgumentError, "negative argument" if n < 0
+ (1..n).map do
+ r = rand(2**self.size)
+ self.select.with_index { |_, i| r[i] == 1 }
+ end
+ end
+end
View
2  lib/core/facets/object.rb
@@ -2,7 +2,7 @@
require_relative 'object/clone.rb'
require_relative 'object/dup.rb'
+require_relative 'object/identity.rb'
require_relative 'object/object_state.rb'
require_relative 'object/replace.rb'
require_relative 'object/try_dup.rb'
-
View
13 lib/core/facets/object/identity.rb
@@ -0,0 +1,13 @@
+class Object
+ # An identity method that provides access to an object's 'self'.
+ #
+ # Example:
+ # [1,2,3,4,5,1,2,2,3].group_by(&:identity)
+ # #=> {1=>[1, 1], 2=>[2, 2, 2], 3=>[3, 3], 4=>[4], 5=>[5]}
+ #
+ # CREDIT: Michael Kohl
+ #
+ def identity
+ self
+ end
+end

No commit comments for this range

Something went wrong with that request. Please try again.