wrandom 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/wrandom.rb +16 -9
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 50f2e1f971bbdfc7812bfaa254674b51db00b30b
|
|
4
|
+
data.tar.gz: dcdeb38ba6dc2c0b1a7581fbda98f289fa34e750
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 85d8f537b7aa97d23e5bd7b162d9c762f61f97cee403ac868cf7ac96e08f8fce38c6dc5f87feda990b632ef9c215fd5b010c30b2f6dc63de9ee6c2b4be9c02ab
|
|
7
|
+
data.tar.gz: 00f9160be5922f93e7998bb63357077690722ec025509b518c0ab92627c3c18131613f8b9dcc11b48428a32326d0b0e45e557e249e1280a694c893f8dba31cc1
|
data/lib/wrandom.rb
CHANGED
|
@@ -1,26 +1,33 @@
|
|
|
1
1
|
class Array
|
|
2
|
-
def wsample(
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
def wsample(*args, &block)
|
|
3
|
+
count, options = \
|
|
4
|
+
if args.size == 0 || args[0].class == Hash
|
|
5
|
+
[nil, args[0]]
|
|
6
|
+
else
|
|
7
|
+
[args[0], args[1]]
|
|
8
|
+
end
|
|
9
|
+
arr = wshuffle(options, &block)
|
|
10
|
+
count ? arr.first(count) : arr.first
|
|
5
11
|
end
|
|
6
12
|
|
|
7
|
-
def wshuffle(&block)
|
|
8
|
-
sort_by { |v| w_algorithm(v, &block) }
|
|
13
|
+
def wshuffle(options = {}, &block)
|
|
14
|
+
sort_by { |v| w_algorithm(v, options, &block) }.reverse!
|
|
9
15
|
end
|
|
10
16
|
|
|
11
|
-
def wshuffle!(&block)
|
|
12
|
-
sort_by! { |v| w_algorithm(v, &block) }
|
|
17
|
+
def wshuffle!(options = {}, &block)
|
|
18
|
+
sort_by! { |v| w_algorithm(v, options, &block) }.reverse!
|
|
13
19
|
end
|
|
14
20
|
|
|
15
21
|
private
|
|
16
22
|
|
|
17
|
-
def w_algorithm(v, &block)
|
|
23
|
+
def w_algorithm(v, options = {}, &block)
|
|
18
24
|
# Pavlos S. Efraimidis, Paul G. Spirakis
|
|
19
25
|
# Weighted random sampling with a reservoir
|
|
20
26
|
# Information Processing Letters
|
|
21
27
|
# Volume 97 Issue 5, 16 March 2006
|
|
22
28
|
# Pages 181-185
|
|
29
|
+
r = options[:random] ? options[:random].rand : rand rescue rand
|
|
23
30
|
population = block.call(v)
|
|
24
|
-
|
|
31
|
+
r ** ( 1.0 / population )
|
|
25
32
|
end
|
|
26
33
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: wrandom
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- juyeong
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2016-
|
|
11
|
+
date: 2016-02-07 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: monkey patching Array class. (weighted shuffle, weighted sample)
|
|
14
14
|
email: dev@jy.is
|