weighted_list 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 79a123c813e3841e14a5e3c69cd9cabad1a235e8a31ad072d0413b633714a6cf
4
+ data.tar.gz: 8517d24ed3f7cb6a0f003fe217532b2cf61e01fbc1b6c41b166d475e1cd6f227
5
+ SHA512:
6
+ metadata.gz: e055b1d48cbe320e0aac31a57a31d270fea8b5b92a7c81e3ec15a1a1b3e26654070262dc4e3221819914c3efe245ba9ab0552aaefdb1f0951977c1a4635d7379
7
+ data.tar.gz: 320b9a7bd0a458983d58e1b74023842524b82c5fd9c35232049c1b17dd99af6b7ee1f3986c3907db4f346e1ce9f776da0752735134201f1dbd106a6d82ad43c6
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .rspec_status
10
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 1.16.5
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in weighted_list.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ weighted_list (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.6.0)
12
+ rspec-core (~> 3.6.0)
13
+ rspec-expectations (~> 3.6.0)
14
+ rspec-mocks (~> 3.6.0)
15
+ rspec-core (3.6.0)
16
+ rspec-support (~> 3.6.0)
17
+ rspec-expectations (3.6.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.6.0)
20
+ rspec-mocks (3.6.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.6.0)
23
+ rspec-support (3.6.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.16)
30
+ rake (~> 10.0)
31
+ rspec (~> 3.0)
32
+ weighted_list!
33
+
34
+ BUNDLED WITH
35
+ 1.16.5
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Jayson Virissimo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # WeightedList [![Build Status](https://travis-ci.org/jaysonvirissimo/weighted_list.svg?branch=master)](https://travis-ci.org/jaysonvirissimo/weighted_list)
2
+
3
+ **WeightedList** attempts to improve on the very popular [weighted_randomizer](https://rubygems.org/gems/weighted_randomizer) gem, by:
4
+
5
+ 1. Sampling *without* replacement, analogous to Ruby's own `Array#sample`
6
+
7
+ 2. Behaving as a typical Ruby collection with `#each`, `#map`, `#sort`, etc...
8
+
9
+ 3. Using any user supplied randomizer that responds to `#rand` with a number
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'weighted_list'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install weighted_list
26
+
27
+ ## Usage
28
+ ```ruby
29
+ list = WeightedList.new({ eastern: 150, central: 92, mountain: 21, pacific: 53 })
30
+
31
+ list.sample # => :pacific
32
+ list.sample(1) # => [:central]
33
+ list.sample(3) # => [:central, :eastern, :pacific]
34
+ list.sample(100) # => [:central, :eastern, :pacific, :mountain]
35
+
36
+ list.map(&:to_s).map(&:capitalize).sort.join(', ') # => "Central, Eastern, Mountain, Pacific"
37
+
38
+ class CustomRandomizer
39
+ def rand(n)
40
+ 1.0
41
+ end
42
+ end
43
+ list.sample(2, random: CustomRandomizer.new) # => [:eastern, :central]
44
+ ```
45
+
46
+ ## Development
47
+
48
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
49
+
50
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jaysonvirissimo/weighted_list.
55
+
56
+ ## License
57
+
58
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "weighted_list"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ class WeightedList
4
+ class Sampler
5
+ def initialize(hash, random: Random)
6
+ @hash = hash.clone
7
+ @random = random
8
+ end
9
+
10
+ def call
11
+ Result.new(chosen, remaining)
12
+ end
13
+
14
+ private
15
+
16
+ attr_reader :hash, :random
17
+
18
+ Result = Struct.new(:chosen, :remaining)
19
+
20
+ def choose
21
+ return if hash.empty?
22
+ current_target = random.rand(total_weight)
23
+
24
+ hash.each do |item, weight|
25
+ return item if current_target <= weight
26
+ current_target -= weight
27
+ end
28
+ end
29
+
30
+ def chosen
31
+ @chosen ||= choose
32
+ end
33
+
34
+ def remaining
35
+ hash.reject { |item, _weight| chosen == item }
36
+ end
37
+
38
+ def total_weight
39
+ @total_weight ||= hash.values.reduce(&:+)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class WeightedList
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'weighted_list/version'
4
+ require 'weighted_list/sampler'
5
+
6
+ class WeightedList
7
+ include Enumerable
8
+
9
+ def initialize(hash)
10
+ @hash = hash
11
+ end
12
+
13
+ def each(&block)
14
+ hash.keys.each(&block)
15
+ end
16
+
17
+ def sample(quantity = nil, random: Random)
18
+ @random = random
19
+ return single_item unless quantity
20
+ (0...quantity).each_with_object(initial_memo) do |_index, memo|
21
+ result = Sampler.new(memo[:current_list], random: random).call
22
+ memo[:chosen].push(result.chosen)
23
+ memo[:current_list] = result.remaining
24
+ end[:chosen].compact
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :hash, :random
30
+
31
+ def initial_memo
32
+ { chosen: [], current_list: hash }
33
+ end
34
+
35
+ def single_item
36
+ Sampler.new(hash, random: random).call.chosen
37
+ end
38
+ end
@@ -0,0 +1,37 @@
1
+
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path('lib', __dir__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require 'weighted_list/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'weighted_list'
10
+ spec.version = WeightedList::VERSION
11
+ spec.authors = ['Jayson Virissimo']
12
+ spec.email = ['jayson.virissimo@asu.edu']
13
+
14
+ spec.summary = 'Sample from a weighted list.'
15
+ spec.description = 'Choose random elements from a weighted list with or without replacement.'
16
+ spec.homepage = 'https://github.com/jaysonvirissimo/weighted_list'
17
+ spec.license = 'MIT'
18
+
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
21
+ else
22
+ raise 'RubyGems >= 2.0 is required to protect against public gem pushes.'
23
+ end
24
+
25
+ # Specify which files should be added to the gem when it is released.
26
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
28
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
29
+ end
30
+ spec.bindir = 'exe'
31
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ['lib']
33
+
34
+ spec.add_development_dependency 'bundler', '~> 1.16'
35
+ spec.add_development_dependency 'rake', '~> 10.0'
36
+ spec.add_development_dependency 'rspec', '~> 3.0'
37
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: weighted_list
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jayson Virissimo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-10-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Choose random elements from a weighted list with or without replacement.
56
+ email:
57
+ - jayson.virissimo@asu.edu
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/weighted_list.rb
73
+ - lib/weighted_list/sampler.rb
74
+ - lib/weighted_list/version.rb
75
+ - weighted_list.gemspec
76
+ homepage: https://github.com/jaysonvirissimo/weighted_list
77
+ licenses:
78
+ - MIT
79
+ metadata:
80
+ allowed_push_host: https://rubygems.org/
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.7.6
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Sample from a weighted list.
101
+ test_files: []