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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/wrandom.rb +16 -9
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ed64f70f455b763cf7cc7b3439f19e989775e2dc
4
- data.tar.gz: 6b22952321638afe581db711720676f4a0a94b99
3
+ metadata.gz: 50f2e1f971bbdfc7812bfaa254674b51db00b30b
4
+ data.tar.gz: dcdeb38ba6dc2c0b1a7581fbda98f289fa34e750
5
5
  SHA512:
6
- metadata.gz: 6b2c71ffa4d00441f1c07cae8250bd12ecda37592a11f14aebbb33f6e4eba1f50e7c0cdf28cca14a976ddbc184b0c919c53046a67a5cc66898cfdb45074d1e5b
7
- data.tar.gz: 842907b3b50c0e3962ca3d3cb16789e59f873c3bb8ba4c6a41371070500e1de369ed5cd5027890b5d615dc6f89cc409a84dc8ee8457879f96537e132d039fb47
6
+ metadata.gz: 85d8f537b7aa97d23e5bd7b162d9c762f61f97cee403ac868cf7ac96e08f8fce38c6dc5f87feda990b632ef9c215fd5b010c30b2f6dc63de9ee6c2b4be9c02ab
7
+ data.tar.gz: 00f9160be5922f93e7998bb63357077690722ec025509b518c0ab92627c3c18131613f8b9dcc11b48428a32326d0b0e45e557e249e1280a694c893f8dba31cc1
@@ -1,26 +1,33 @@
1
1
  class Array
2
- def wsample(count = nil, &block)
3
- ret = max_by(count || 1) { |v| w_algorithm(v, &block) }
4
- count ? ret : ret[0]
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
- rand ** ( 1.0 / population )
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.1
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-01-30 00:00:00.000000000 Z
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