weighted_list 0.5.0 → 0.8.2

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: 555c81c41659a9b521328efe8a369460ca51db7f6fa80b9ea425354ae98a3e80
4
+ data.tar.gz: 479301f4950b0cd0d99140bae561bfe42f89fe4579774d85c9a37736826cd5cc
5
5
  SHA512:
6
- metadata.gz: ba0ec48511362ad411da0188ed6680fd58edf866141a23bfe2d71c70b51d4f74965c930187ed27537d27a39466516d93e47bdcff8498700a7561091f0e4b86cc
7
- data.tar.gz: c0f9c8f9a7058396fa711973f04abe04de40bf09f03ac08f6ddadfac3a94c131fc6e563ecb36171918c170a7e59b4d5d9e59dafce39c07a17ad422cb3eb65dbc
6
+ metadata.gz: 0c03ed7714d0dee0d40e3d70b90c84b5c59f603387c9a2c7535bf92adac4353264b0bb6546fa63878e55af849a0441989bb2b0585a0fef5990429db35b305eb2
7
+ data.tar.gz: 635ab3ff9ba2f452232d84c9f03b9cd26106faf753f77bba061d08d3132bee0ce4f75868febfe47604fe1a4d02117dd1a53179ffaf3b5a265f5abcb886026dcd
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.0.2
data/Gemfile.lock CHANGED
@@ -1,13 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- weighted_list (0.5.0)
4
+ weighted_list (0.8.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  diff-lcs (1.3)
10
- rake (10.5.0)
10
+ rake (13.0.1)
11
11
  rspec (3.6.0)
12
12
  rspec-core (~> 3.6.0)
13
13
  rspec-expectations (~> 3.6.0)
@@ -26,10 +26,10 @@ PLATFORMS
26
26
  ruby
27
27
 
28
28
  DEPENDENCIES
29
- bundler (~> 1.16)
30
- rake (~> 10.0)
29
+ bundler (>= 2.2.10)
30
+ rake (~> 13.0)
31
31
  rspec (~> 3.0)
32
32
  weighted_list!
33
33
 
34
34
  BUNDLED WITH
35
- 1.16.5
35
+ 2.2.22
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,14 @@ 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]
61
+
62
+ # If you want to mutate the original list:
63
+ list.shuffle!
53
64
  ```
54
65
 
55
66
  ## 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.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,18 @@ 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
+
36
+ def shuffle!(random: Random)
37
+ sample(hash.length, random: random).tap do |returned_collection|
38
+ returned_collection.each_with_object({}) do |item, replace_with|
39
+ replace_with[item] = hash[item]
40
+ end.tap { |replace_with| @hash = replace_with }
41
+ end
42
+ end
43
+
32
44
  private
33
45
 
34
46
  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.8.2'
5
5
  end
@@ -31,7 +31,7 @@ Gem::Specification.new do |spec|
31
31
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
32
  spec.require_paths = ['lib']
33
33
 
34
- spec.add_development_dependency 'bundler', '~> 1.16'
35
- spec.add_development_dependency 'rake', '~> 10.0'
34
+ spec.add_development_dependency 'bundler', '>= 2.2.10'
35
+ spec.add_development_dependency 'rake', '~> 13.0'
36
36
  spec.add_development_dependency 'rspec', '~> 3.0'
37
37
  end
metadata CHANGED
@@ -1,43 +1,43 @@
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.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jayson Virissimo
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-03 00:00:00.000000000 Z
11
+ date: 2021-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.16'
19
+ version: 2.2.10
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.16'
26
+ version: 2.2.10
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '13.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '13.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -61,6 +61,7 @@ extra_rdoc_files: []
61
61
  files:
62
62
  - ".gitignore"
63
63
  - ".rspec"
64
+ - ".ruby-version"
64
65
  - ".travis.yml"
65
66
  - Gemfile
66
67
  - Gemfile.lock
@@ -79,7 +80,7 @@ licenses:
79
80
  - MIT
80
81
  metadata:
81
82
  allowed_push_host: https://rubygems.org/
82
- post_install_message:
83
+ post_install_message:
83
84
  rdoc_options: []
84
85
  require_paths:
85
86
  - lib
@@ -94,9 +95,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
95
  - !ruby/object:Gem::Version
95
96
  version: '0'
96
97
  requirements: []
97
- rubyforge_project:
98
- rubygems_version: 2.7.6
99
- signing_key:
98
+ rubygems_version: 3.2.22
99
+ signing_key:
100
100
  specification_version: 4
101
101
  summary: Sample from a weighted list.
102
102
  test_files: []