weighted_list 0.5.0 → 0.7.0
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/Gemfile.lock +1 -1
- data/README.md +11 -3
- data/lib/weighted_list.rb +5 -1
- data/lib/weighted_list/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d5b3840de4d609e646fb51214fd8ee68ffb538878213e5d3a03e59d53410f7b
|
4
|
+
data.tar.gz: 6a6a085e2d086c4ae4ca00e7dacecd3350f1af2f98cd910f3c1d8a0be07c3c15
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f52a7a8bdc0e912069f1ef71ce74220c24d720e71c8d3faadc328f5398127daac959a327cb1b9268e9ed9a227892a214b80816d46ef22fcfa8318e86dd1d7044
|
7
|
+
data.tar.gz: d79b5c017734bb73f73392d280a0d745f50c868b2c56a3b826d69f7b93db929dd1d633a415c9bb6244ffeacb47e1016c01a881d5c889d6909805d244a01638f1
|
data/Gemfile.lock
CHANGED
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.
|
7
|
+
2. Implementing a weighted shuffle
|
8
8
|
|
9
|
-
3.
|
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
|
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
|
data/lib/weighted_list.rb
CHANGED
@@ -16,7 +16,7 @@ class WeightedList
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def each(&block)
|
19
|
-
hash.
|
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
|
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.
|
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-
|
11
|
+
date: 2018-12-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|