weighted_list 0.5.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0a8ad1ca4f95df917bd99628170dfc4b7571633216e6b3637504135ab3fb759e
4
- data.tar.gz: cff9e4ee6597b77a60cbbd04abfe39f84cce89ed787d898a3a627ad5638664ad
3
+ metadata.gz: 5d5b3840de4d609e646fb51214fd8ee68ffb538878213e5d3a03e59d53410f7b
4
+ data.tar.gz: 6a6a085e2d086c4ae4ca00e7dacecd3350f1af2f98cd910f3c1d8a0be07c3c15
5
5
  SHA512:
6
- metadata.gz: ba0ec48511362ad411da0188ed6680fd58edf866141a23bfe2d71c70b51d4f74965c930187ed27537d27a39466516d93e47bdcff8498700a7561091f0e4b86cc
7
- data.tar.gz: c0f9c8f9a7058396fa711973f04abe04de40bf09f03ac08f6ddadfac3a94c131fc6e563ecb36171918c170a7e59b4d5d9e59dafce39c07a17ad422cb3eb65dbc
6
+ metadata.gz: f52a7a8bdc0e912069f1ef71ce74220c24d720e71c8d3faadc328f5398127daac959a327cb1b9268e9ed9a227892a214b80816d46ef22fcfa8318e86dd1d7044
7
+ data.tar.gz: d79b5c017734bb73f73392d280a0d745f50c868b2c56a3b826d69f7b93db929dd1d633a415c9bb6244ffeacb47e1016c01a881d5c889d6909805d244a01638f1
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- weighted_list (0.5.0)
4
+ weighted_list (0.7.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -4,9 +4,11 @@
4
4
 
5
5
  1. Sampling *without* replacement, analogous to Ruby's own `Array#sample`
6
6
 
7
- 2. Behaving as a typical Ruby collection with `#each`, `#map`, `#sort`, etc...
7
+ 2. Implementing a weighted shuffle
8
8
 
9
- 3. Using any user supplied randomizer that responds to `#rand` with a number
9
+ 3. Accepting an external source of randomness for easy deterministic testing
10
+
11
+ 4. Behaving like other Ruby collections with `#each`, `#map`, `#sort`, etc...
10
12
 
11
13
  ## Installation
12
14
 
@@ -40,7 +42,8 @@ list.sample(100) # => [:central, :eastern, :pacific, :mountain]
40
42
  # It acts like a proper Ruby collection should:
41
43
  list.map(&:to_s).map(&:capitalize).sort.join(', ') # => "Central, Eastern, Mountain, Pacific"
42
44
 
43
- # For when you'd like to provide your own entropy or cleanly test deterministically.
45
+ # For when you'd like to provide your own entropy (perhaps for deterministic tests):
46
+ # Just make sure the object you pass responds to `rand`
44
47
  class CustomRandomizer
45
48
  def rand(n)
46
49
  1.0
@@ -50,6 +53,11 @@ list.sample(2, random: CustomRandomizer.new) # => [:eastern, :central]
50
53
 
51
54
  # If you want to allow repeats:
52
55
  list.sample(4, with_replacement: true) # => [:eastern, :mountain, :eastern, :eastern]
56
+
57
+ # Shuffle the list while still respecting the weights
58
+ list.shuffle # => [:central, :eastern, :mountain, :pacific]
59
+ list.shuffle # => [:pacific, :eastern, :central, :mountain]
60
+ list.shuffle # => [:pacific, :central, :eastern, :mountain]
53
61
  ```
54
62
 
55
63
  ## Development
@@ -16,7 +16,7 @@ class WeightedList
16
16
  end
17
17
 
18
18
  def each(&block)
19
- hash.keys.each(&block)
19
+ hash.each_key(&block)
20
20
  end
21
21
 
22
22
  def sample(quantity = nil, random: Random, with_replacement: false)
@@ -29,6 +29,10 @@ class WeightedList
29
29
  end[:selected]
30
30
  end
31
31
 
32
+ def shuffle(random: Random)
33
+ sample(hash.length, random: random)
34
+ end
35
+
32
36
  private
33
37
 
34
38
  attr_reader :hash, :random
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class WeightedList
4
- VERSION = '0.5.0'
4
+ VERSION = '0.7.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weighted_list
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jayson Virissimo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-03 00:00:00.000000000 Z
11
+ date: 2018-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler