nondeterminism 0.1.0 → 0.2.1

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.
Files changed (39) hide show
  1. data/History.txt +28 -0
  2. data/License.txt +17 -0
  3. data/Manifest.txt +37 -0
  4. data/README.txt +1 -0
  5. data/Rakefile +4 -0
  6. data/config/hoe.rb +71 -0
  7. data/config/requirements.rb +17 -0
  8. data/lib/nondeterminism.rb +22 -14
  9. data/lib/nondeterminism/array.rb +10 -0
  10. data/lib/nondeterminism/probability/event.rb +29 -0
  11. data/lib/nondeterminism/probability/event_chain.rb +42 -0
  12. data/lib/nondeterminism/probability/helper_methods.rb +17 -0
  13. data/lib/nondeterminism/probability/kernel.rb +10 -0
  14. data/lib/nondeterminism/probability/numeric.rb +8 -0
  15. data/lib/nondeterminism/range.rb +10 -0
  16. data/lib/nondeterminism/version.rb +9 -0
  17. data/log/debug.log +0 -0
  18. data/script/destroy +14 -0
  19. data/script/generate +14 -0
  20. data/script/txt2html +74 -0
  21. data/setup.rb +1585 -0
  22. data/tasks/deployment.rake +34 -0
  23. data/tasks/environment.rake +7 -0
  24. data/tasks/website.rake +17 -0
  25. data/test/{array_test.rb → test_array.rb} +1 -2
  26. data/test/test_helper.rb +2 -1
  27. data/test/{integer_generator_test.rb → test_integer_generator.rb} +10 -3
  28. data/test/{percent_probability_test.rb → test_percent_probability.rb} +1 -2
  29. data/test/{probability_event_chain_test.rb → test_probability_event_chain.rb} +1 -3
  30. data/test/{probability_generator_test.rb → test_probability_generator.rb} +1 -2
  31. data/test/{range_test.rb → test_range.rb} +1 -2
  32. data/test/{ratio_probability_test.rb → test_ratio_probability.rb} +1 -2
  33. data/website/index.html +181 -0
  34. data/website/index.txt +111 -0
  35. data/website/javascripts/rounded_corners_lite.inc.js +285 -0
  36. data/website/stylesheets/screen.css +138 -0
  37. data/website/template.rhtml +48 -0
  38. metadata +60 -21
  39. data/README +0 -86
data/History.txt ADDED
@@ -0,0 +1,28 @@
1
+ == 0.1.0 2008-01-30
2
+
3
+ * 9 major enhancements:
4
+ * with_probability(x) {...}
5
+ * with_probability(x) {...}.else {...}
6
+ * with_probability(x) {...}.else_with_probability(y) {...}
7
+ * with_probability(x) {...}.else_with_probability(y) {...}...else {...}
8
+ * x.percent_of_the_time {...}
9
+ * x.percent_of_the_time {...}.else {...}
10
+ * [a, b, ...].random_element
11
+ * [a, b, ...].rand
12
+ * (x..y).times
13
+
14
+ == 0.2.0 2008-01-30
15
+
16
+ * 1 major change:
17
+ * changed project file structure to match rubygems / newgem standards
18
+
19
+ == 0.2.1 2008-01-30
20
+
21
+ * 1 minor fix:
22
+ * fixed Nondeterminism::DEFAULT_INTEGER_GENERATOR
23
+
24
+ * 1 minor enhancement:
25
+ * Nondeterminism::DEFAULT_INTEGER_GENERATOR now relies on Nondeterminism::DEFAULT_PROBABILITY_GENERATOR
26
+
27
+ * 1 minor change:
28
+ * moved all documentation from README.txt to website/index.txt (or .html)
data/License.txt ADDED
@@ -0,0 +1,17 @@
1
+ Copyright 2008 James Rosen
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
14
+
15
+
16
+
17
+
data/Manifest.txt ADDED
@@ -0,0 +1,37 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ config/hoe.rb
7
+ config/requirements.rb
8
+ lib/nondeterminism.rb
9
+ lib/nondeterminism/array.rb
10
+ lib/nondeterminism/probability/event.rb
11
+ lib/nondeterminism/probability/event_chain.rb
12
+ lib/nondeterminism/probability/helper_methods.rb
13
+ lib/nondeterminism/probability/kernel.rb
14
+ lib/nondeterminism/probability/numeric.rb
15
+ lib/nondeterminism/range.rb
16
+ lib/nondeterminism/version.rb
17
+ log/debug.log
18
+ script/destroy
19
+ script/generate
20
+ script/txt2html
21
+ setup.rb
22
+ tasks/deployment.rake
23
+ tasks/environment.rake
24
+ tasks/website.rake
25
+ test/test_array.rb
26
+ test/test_helper.rb
27
+ test/test_integer_generator.rb
28
+ test/test_percent_probability.rb
29
+ test/test_probability_event_chain.rb
30
+ test/test_probability_generator.rb
31
+ test/test_range.rb
32
+ test/test_ratio_probability.rb
33
+ website/index.html
34
+ website/index.txt
35
+ website/javascripts/rounded_corners_lite.inc.js
36
+ website/stylesheets/screen.css
37
+ website/template.rhtml
data/README.txt ADDED
@@ -0,0 +1 @@
1
+ see website/index.html
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'config/requirements'
2
+ require 'config/hoe' # setup Hoe + all gem configuration
3
+
4
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
data/config/hoe.rb ADDED
@@ -0,0 +1,71 @@
1
+ require 'nondeterminism/version'
2
+
3
+ AUTHOR = 'James Rosen' # can also be an array of Authors
4
+ EMAIL = "james @nospam@ u-presence.com"
5
+ DESCRIPTION = "probabilistic blocks, random elements"
6
+ GEM_NAME = 'nondeterminism'
7
+ RUBYFORGE_PROJECT = 'nondeterminism'
8
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
9
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
10
+
11
+ @config_file = "~/.rubyforge/user-config.yml"
12
+ @config = nil
13
+ RUBYFORGE_USERNAME = "gcnovus"
14
+ def rubyforge_username
15
+ unless @config
16
+ begin
17
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
18
+ rescue
19
+ puts <<-EOS
20
+ ERROR: No rubyforge config file found: #{@config_file}
21
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
22
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
23
+ EOS
24
+ exit
25
+ end
26
+ end
27
+ RUBYFORGE_USERNAME.replace @config["username"]
28
+ end
29
+
30
+
31
+ REV = nil
32
+ # UNCOMMENT IF REQUIRED:
33
+ # REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
34
+ VERS = Nondeterminism::VERSION::STRING + (REV ? ".#{REV}" : "")
35
+ RDOC_OPTS = ['--quiet', '--title', 'nondeterminism documentation',
36
+ "--opname", "index.html",
37
+ "--line-numbers",
38
+ "--main", "README",
39
+ "--inline-source"]
40
+
41
+ class Hoe
42
+ def extra_deps
43
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
44
+ @extra_deps
45
+ end
46
+ end
47
+
48
+ # Generate all the Rake tasks
49
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
50
+ hoe = Hoe.new(GEM_NAME, VERS) do |p|
51
+ p.author = AUTHOR
52
+ p.description = DESCRIPTION
53
+ p.email = EMAIL
54
+ p.summary = DESCRIPTION
55
+ p.url = HOMEPATH
56
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
57
+ p.test_globs = ["test/**/test_*.rb"]
58
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
59
+
60
+ # == Optional
61
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
62
+ #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
63
+
64
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
65
+
66
+ end
67
+
68
+ CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
69
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
70
+ hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
71
+ hoe.rsync_args = '-av --delete --ignore-errors'
@@ -0,0 +1,17 @@
1
+ require 'fileutils'
2
+ include FileUtils
3
+
4
+ require 'rubygems'
5
+ %w[rake hoe newgem rubigen].each do |req_gem|
6
+ begin
7
+ require req_gem
8
+ rescue LoadError
9
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
10
+ puts "Installation: gem install #{req_gem} -y"
11
+ exit
12
+ end
13
+ end
14
+
15
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
16
+
17
+ require 'nondeterminism'
@@ -1,3 +1,5 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
1
3
  require 'nondeterminism/probability/kernel'
2
4
  require 'nondeterminism/probability/numeric'
3
5
  require 'nondeterminism/array'
@@ -5,48 +7,54 @@ require 'nondeterminism/range'
5
7
 
6
8
  module Nondeterminism
7
9
 
10
+ unless self.const_defined?(:DEFAULT_PROBABILITY_GENERATOR)
8
11
  DEFAULT_PROBABILITY_GENERATOR = Proc.new { Kernel.rand }
9
-
10
- @@probability_generator = DEFAULT_PROBABILITY_GENERATOR
11
-
12
+
13
+ @@probability_generator = Nondeterminism::DEFAULT_PROBABILITY_GENERATOR
14
+
12
15
  def self.probability_generator
13
16
  @@probability_generator
14
17
  end
15
-
18
+
16
19
  def self.probability_generator=(generator)
17
20
  raise ArgumentError.new('generator must respond to #to_proc') unless generator.respond_to?(:to_proc)
18
21
  @@probability_generator = generator.to_proc
19
22
  end
20
-
23
+
21
24
  def self.random_probability
22
25
  result = probability_generator.call
23
26
  raise "generator (#{probability_generator}) generated invalid probability: #{result}" unless result >= 0.0 && result <= 1.0
24
27
  result
25
28
  end
26
-
27
-
28
- DEFAULT_INTEGER_GENERATOR = Proc.new { |low, high| Kernel.rand * high + low }
29
-
30
- @@integer_generator = DEFAULT_PROBABILITY_GENERATOR
31
-
29
+ end
30
+
31
+
32
+
33
+
34
+ unless self.const_defined?(:DEFAULT_INTEGER_GENERATOR)
35
+ DEFAULT_INTEGER_GENERATOR = Proc.new { |low, high| Nondeterminism.random_probability * (high + 1 - low) + low }
36
+
37
+ @@integer_generator = Nondeterminism::DEFAULT_INTEGER_GENERATOR
38
+
32
39
  def self.integer_generator
33
40
  @@integer_generator
34
41
  end
35
-
42
+
36
43
  def self.integer_generator=(generator)
37
44
  raise ArgumentError.new('generator must respond to #to_proc') unless generator.respond_to?(:to_proc)
38
45
  proc = generator.to_proc
39
46
  raise ArgumentError.new('generator must have arity == 2 or < -2') unless proc.arity == 2 || proc.arity < -2
40
47
  @@integer_generator = proc
41
48
  end
42
-
49
+
43
50
  def self.random_integer(range)
44
51
  self.random_integer(range.first, range.last)
45
52
  end
46
-
53
+
47
54
  def self.random_integer(low, high)
48
55
  result = integer_generator.call(low, high).to_i
49
56
  raise "generator (#{integer_generator}) generated invalid integer: #{result}" unless result >= low && result <= high
50
57
  result
51
58
  end
59
+ end
52
60
  end
@@ -0,0 +1,10 @@
1
+ Array.class_eval do
2
+
3
+ def rand
4
+ return nil if empty?
5
+ self[Nondeterminism::random_integer(0, self.size - 1)]
6
+ end
7
+
8
+ alias_method :random_element, :rand
9
+
10
+ end
@@ -0,0 +1,29 @@
1
+ require 'nondeterminism/probability/helper_methods'
2
+ require 'nondeterminism/probability/event_chain'
3
+
4
+ module Nondeterminism
5
+ module Probability
6
+ class Event
7
+
8
+ include Nondeterminism::Probability::HelperMethods
9
+
10
+ attr_reader :probability
11
+
12
+ def initialize(probability, &block)
13
+ validate_probability(:probability, probability)
14
+ raise ArgumentError.new('no block given') unless block_given?
15
+ @probability = probability
16
+ @block = block
17
+ end
18
+
19
+ def call
20
+ random = random_probability
21
+ if random <= probability
22
+ @block.call
23
+ end
24
+ return Nondeterminism::Probability::EventChain.new(probability, random)
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,42 @@
1
+ require 'nondeterminism/probability/helper_methods'
2
+
3
+ module Nondeterminism
4
+ module Probability
5
+ class EventChain
6
+
7
+ include Nondeterminism::Probability::HelperMethods
8
+
9
+ attr_reader :total_probability, :random_value
10
+
11
+ # a new chain, with random value +random+
12
+ def initialize(total_probability = 0.0, random_value = nil)
13
+ @total_probability = total_probability
14
+ @random_value = random_value || random_probability
15
+ end
16
+
17
+ def else_with_probability(probability, &block)
18
+ raise ArgumentError.new('no block given') unless block_given?
19
+ validate_probability(:probability, probability)
20
+ new_total = total_probability + probability
21
+ raise ArgumentError.new("total probability (#{new_total}) cannot exceed 1.0") if new_total > 1.0
22
+
23
+ if !completed? && random_value <= new_total
24
+ yield block
25
+ end
26
+ @total_probability = new_total
27
+ return self
28
+ end
29
+
30
+ def else(&block)
31
+ raise ArgumentError.new('no block given') unless block_given?
32
+ yield block unless completed?
33
+ return nil
34
+ end
35
+
36
+ def completed?
37
+ random_value <= total_probability
38
+ end
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,17 @@
1
+ require 'nondeterminism'
2
+
3
+ module Nondeterminism
4
+ module Probability
5
+ module HelperMethods
6
+
7
+ def validate_probability(sym, value)
8
+ raise ArgumentError.new("#{sym} (#{value}) must be between 0.0 and 1.0 (inclusive)") unless value >= 0.0 && value <= 1.0
9
+ end
10
+
11
+ def random_probability
12
+ Nondeterminism::random_probability
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ require 'nondeterminism/probability/event'
2
+
3
+ Kernel.class_eval do
4
+ def with_probability(prob, &block)
5
+ raise(ArgumentError, 'probability must be between 0.0 and 1.0 (inclusive)') unless prob >= 0.0 && prob <= 1.0
6
+ return Nondeterminism::Probability::Event.new(prob, &block).call
7
+ end
8
+
9
+ alias_method :with_likelihood, :with_probability
10
+ end
@@ -0,0 +1,8 @@
1
+ Numeric.class_eval do
2
+
3
+ def percent_of_the_time(&block)
4
+ raise(ArgumentError, 'percent must be between 0 and 100 (inclusive)') unless self >= 0.0 && self <= 100.0
5
+ with_probability(self / 100.0, &block)
6
+ end
7
+
8
+ end
@@ -0,0 +1,10 @@
1
+ Range.class_eval do
2
+
3
+ # ex: (3..6).times do
4
+ def times(&block)
5
+ raise "#{self} contains negative indices" if self.first < 0 || self.last < 0
6
+ raise ArgumentError.new('no block given') unless block_given?
7
+ self.to_a.rand.times(&block)
8
+ end
9
+
10
+ end
@@ -0,0 +1,9 @@
1
+ module Nondeterminism #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 2
5
+ TINY = 1
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
data/log/debug.log ADDED
File without changes
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.join(File.dirname(__FILE__), '..')
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.join(File.dirname(__FILE__), '..')
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)