pickup 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pickup.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 fl00r
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,77 @@
1
+ # Pickup
2
+
3
+ Pickup helps you to pick item from collection by it's weight/probability
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'pickup'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install pickup
18
+
19
+ ## Usage
20
+
21
+ For example, we have got a pond with fish.
22
+
23
+ ```ruby
24
+ pond = {
25
+ "selmon" => 1,
26
+ "carp" => 4,
27
+ "crucian" => 3,
28
+ "herring" => 6,
29
+ "sturgeon" => 8,
30
+ "gudgeon" => 10,
31
+ "minnow" => 20
32
+ }
33
+ ```
34
+ Values are a chence to get this fish.
35
+
36
+ So we should create our pickup.
37
+
38
+ ```ruby
39
+ pickup = Pickup.new(pond)
40
+ pickup.pick(3)
41
+ #=> [ "gudgeon", "minnow", "minnow" ]
42
+ ```
43
+ Look, we've just catched few minnows! To get selmon we need some mor tries ;)
44
+
45
+ Ok. What if our probability is not a linear function. We can create our pickup with a function:
46
+
47
+ ```ruby
48
+ pickup = Pickup.new(pond){ |v| v**2 }
49
+ pickup.pick(3)
50
+ #=> ["carp", "selmon", "crucian"]
51
+ ```
52
+ Wow, good catch!
53
+
54
+ Also you can change our `function` on the fly. Let's make square function:
55
+
56
+ ```ruby
57
+ pickup = Pickup.new(pond)
58
+ pickup.pick_func = Proc.new{ |v| v**2 }
59
+ ```
60
+ Or you can pass a block as a probability function wich will be applicable only to current operation
61
+
62
+ ```ruby
63
+ pickup = Pickup.new(pond)
64
+ pickup.pick{ |v| 1/v } # same as pickup.pick(1){ ... }
65
+ #=> "selmon"
66
+ pickup.pick
67
+ #=> "minnow"
68
+ ```
69
+
70
+
71
+ ## Contributing
72
+
73
+ 1. Fork it
74
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
75
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
76
+ 4. Push to the branch (`git push origin my-new-feature`)
77
+ 5. Create new Pull Request
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ task :default => :spec
6
+
7
+ desc 'Tests'
8
+ Rake::TestTask.new(:spec) do |t|
9
+ t.libs << 'spec'
10
+ t.pattern = 'spec/**/*_spec.rb'
11
+ t.verbose = false
12
+ end
@@ -0,0 +1,74 @@
1
+ require "pickup/version"
2
+
3
+ class Pickup
4
+ attr_reader :list
5
+ attr_writer :pick_func
6
+
7
+ def initialize(list, &block)
8
+ @list = list
9
+ @pick_func = block if block_given?
10
+ end
11
+
12
+ def pick(count=1, &block)
13
+ func = block || pick_func
14
+ mlist = MappedList.new(list, func)
15
+ result = count.times.map do |i|
16
+ mlist.random
17
+ end
18
+ count == 1 ? result.first : result
19
+ end
20
+
21
+ def pick_func
22
+ @pick_func ||= begin
23
+ Proc.new do |val|
24
+ val
25
+ end
26
+ end
27
+ end
28
+
29
+ class MappedList
30
+ attr_reader :list, :func, :uniq
31
+
32
+ def initialize(list, func, uniq=false)
33
+ @func = func
34
+ @uniq = uniq
35
+ @list = map_list(list)
36
+ end
37
+
38
+ def map_list(list)
39
+ n = 0
40
+ mapped_list = {}
41
+ list.each do |k, v|
42
+ n += v
43
+ mapped_list[k] = {}
44
+ mapped_list[k][:value] = n
45
+ mapped_list[k][:picked] = false if uniq
46
+ end
47
+ mapped_list
48
+ end
49
+
50
+ def max
51
+ @max ||= begin
52
+ list.inject(0){|sum, (k,v)| sum+list[k][:value] }
53
+ end
54
+ end
55
+
56
+ def random
57
+ num = rand(max)
58
+ get_random_item(num)
59
+ end
60
+
61
+ def get_random_item(n)
62
+ item = list.detect do |k,v|
63
+ val = func.call v[:value]
64
+ num = func.call n
65
+ val >= num && !(uniq && v[:picked])
66
+ end
67
+ item ||= list.first{ |k,v| !v[:picked] }
68
+ raise "No items left" unless item
69
+ key = item[0]
70
+ list[key][:picked] = true if uniq
71
+ key
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,3 @@
1
+ class Pickup
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/pickup/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["fl00r"]
6
+ gem.email = ["fl00r@yandex.ru"]
7
+ gem.description = %q{Pickup helps you to pick item from collection by it's weight/probability}
8
+ gem.summary = %q{Pickup helps you to pick item from collection by it's weight/probability}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "pickup"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Pickup::VERSION
17
+ end
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Pickup do
5
+ before do
6
+ @list = {
7
+ "selmon" => 1,
8
+ "carp" => 4,
9
+ "crucian" => 3,
10
+ "herring" => 6,
11
+ "sturgeon" => 8,
12
+ "gudgeon" => 10,
13
+ "minnow" => 20
14
+ }
15
+ @func = Proc.new{ |a| a }
16
+ @pickup = Pickup.new(@list)
17
+ end
18
+
19
+ it "should be equal" do
20
+ @pickup.pick(2).size.must_equal 2
21
+ end
22
+
23
+ describe Pickup::MappedList do
24
+ before do
25
+ @mapped_list = {"selmon"=>{:value=>1, :picked=>false}, "carp"=>{:value=>5, :picked=>false}, "crucian"=>{:value=>8, :picked=>false}, "herring"=>{:value=>14, :picked=>false}, "sturgeon"=>{:value=>22, :picked=>false}, "gudgeon"=>{:value=>32, :picked=>false}, "minnow"=>{:value=>52, :picked=>false}}
26
+ @ml = Pickup::MappedList.new(@list, @func, true)
27
+ @ml2 = Pickup::MappedList.new(@list, @func)
28
+ end
29
+ it "should return mapped list" do
30
+ @ml.list.must_equal(@mapped_list)
31
+ end
32
+
33
+ it "should return selmon and then carp and then crucian" do
34
+ @ml.get_random_item(1).must_equal "selmon"
35
+ @ml.get_random_item(1).must_equal "carp"
36
+ @ml.get_random_item(1).must_equal "crucian"
37
+ end
38
+
39
+ it "should return selmon 3 times" do
40
+ @ml2.get_random_item(1).must_equal "selmon"
41
+ @ml2.get_random_item(1).must_equal "selmon"
42
+ @ml2.get_random_item(1).must_equal "selmon"
43
+ end
44
+
45
+ it "should return first item after last" do
46
+ @ml.get_random_item(30).must_equal "gudgeon"
47
+ @ml.get_random_item(30).must_equal "minnow"
48
+ @ml.get_random_item(30).must_equal "selmon"
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ require 'pickup'
2
+ require 'minitest/spec'
3
+ require 'minitest/autorun'
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pickup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - fl00r
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-29 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Pickup helps you to pick item from collection by it's weight/probability
15
+ email:
16
+ - fl00r@yandex.ru
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - lib/pickup.rb
27
+ - lib/pickup/version.rb
28
+ - pickup.gemspec
29
+ - spec/pickup/pickup_spec.rb
30
+ - spec/spec_helper.rb
31
+ homepage: ''
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.23
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Pickup helps you to pick item from collection by it's weight/probability
55
+ test_files:
56
+ - spec/pickup/pickup_spec.rb
57
+ - spec/spec_helper.rb