quick-sampler 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 166249b4d99f3c75f161df7c7fcef6b0b544d407
4
- data.tar.gz: efe98dc471d9e954c0159475dd2c13a8ea60c40a
3
+ metadata.gz: fb39b55d1e1f2a633a7b3caae39a0f0908c4de9a
4
+ data.tar.gz: 2e9d62fabcd8657a637fb1ea0b16946af929eee1
5
5
  SHA512:
6
- metadata.gz: 1811619ab4d68251fd2f36ebf7bf23642288162dda1b2e24654e88141b69d543b72366ecd85134e8e93075873716a6547c794c7dfaa1dd8fc06a09df2cdfff17
7
- data.tar.gz: 169017a885dc058a33b6cea98587c59824f6e97e9ed82c17ce121ad2a9f753bb5f47823a5a30649a9fb8ab8766dd25f957fd47bf7c62389af3dddc114952e18b
6
+ metadata.gz: 5182a2e7a859f535df1bfe10cc004efe6cef7beb59d124ee8e8275abcddb508cc092af3516bcc202f094279e9716a39316ed0dacadd3d7ae0e3568624e27f21c
7
+ data.tar.gz: ba991af6e96aa6cfa743aadd4475a8b256c92d2f959c32de0e9a42597fd3e38dff4809471b2befc7cbd2c9cf4168a5af17d1bc067c089b5c092d0e90a80b2c43
data/README.md CHANGED
@@ -53,7 +53,7 @@ But [will it blend?][8]
53
53
  [8]: https://github.com/jessitron/gerald#gerald
54
54
 
55
55
  ```irb
56
- pry> sampler2 = Quick::Sampler.compile { config(upper_bound: 5).string(:lower) }
56
+ pry> sampler2 = Quick::Sampler.compile { string(:lower, size: 3..5) }
57
57
  => #<Enumerator::Lazy: #<Enumerator: #<Enumerator::Generator:0x007f295fd88058>:each>>
58
58
  pry> sampler2.zip(sampler).first(5).to_h
59
59
  => {"bjm"=>-4027257104748747508,
@@ -88,15 +88,6 @@ Generators.one_of_weighted Generators.integer => 10,
88
88
  Generators.vector_of(5, Generators.integer) => 5
89
89
  ```
90
90
 
91
- ### Sampler configuration
92
-
93
- Some sampling parameters can be passed as arguments to a sampler function (like
94
- character class `:lower` in the example above). Others - that affect multiple
95
- sub-samplers in a definition - may be injected with a call to `config(...)` (like
96
- `upper_bound` above).
97
-
98
- (**Rosetta stone:** `upper_bound` is what in Haskell QuickCheck is known as `size`)
99
-
100
91
  ### Sampler composability
101
92
 
102
93
  The composition using Enumerable API as demonstrated above is pretty flexible and familiar to a
@@ -6,15 +6,13 @@ module Quick
6
6
  # passed to {Quick::Sampler.compile}.
7
7
  class DSL::Fluidiom < SimpleDelegator
8
8
  # SimpleDelegator so that it can unwrap the "original" sampler with `#__getobj__`
9
- include Quick::Sampler::Config
10
9
 
11
10
  # @api private
12
11
  # wraps a `sampler` into a `Fluidiom` instance so it has extra methods while
13
12
  # inside the block passed to {Quick::Sampler.compile}
14
- def initialize sampler, _config = {}
13
+ def initialize sampler
15
14
  sampler = Base.new(sampler) unless sampler.is_a? Base
16
15
  super(sampler)
17
- config.merge! _config
18
16
  end
19
17
 
20
18
  # @return [Quick::Sampler]
@@ -24,16 +22,15 @@ module Quick
24
22
  end
25
23
 
26
24
  # @return [Quick::Sampler]
27
- # wrapped sampler that starts out with the same config as this one
25
+ # a new fluidiom-wrapped sampler
28
26
  def spawn sampler
29
- self.class.new(sampler, config)
27
+ self.class.new(sampler)
30
28
  end
31
29
 
32
30
  # spawn a filtering sampler
33
31
  #
34
- # The produced sampler honors the config variable `max_iterations` and stops
35
- # iterating when that many original values are tested.
36
- #
32
+ # @param [Integer] max_iterations
33
+ # try at most this many values before giving up
37
34
  # @return [Quick::Sampler]
38
35
  # a sampler that passes through only samples that satisfy the
39
36
  # predicate given as block
@@ -41,7 +38,7 @@ module Quick
41
38
  # a sampled value to be tested
42
39
  # @yieldreturn [Boolean]
43
40
  # `true` to pass the value through
44
- def such_that &predicate
41
+ def such_that max_iterations: 1000, &predicate
45
42
  spawn(unwrap.take(max_iterations).select(&predicate))
46
43
  end
47
44
 
@@ -45,9 +45,6 @@ module Quick
45
45
 
46
46
  # Sampler of uniform arrays
47
47
  #
48
- # This sampler honors `upper_bound` config variable and samples Arrays of up to
49
- # that many elements.
50
- #
51
48
  # **Rosetta stone** the single argument version corresponds to QuickCheck's `listOf`.
52
49
  # Passing `non_empty: true` turns it into QuickCheck's `listOf1`.
53
50
  #
@@ -55,25 +52,22 @@ module Quick
55
52
  # a sampler that produces arrays of values sampled from its argument
56
53
  # @param [Quick::Sampler] sampler
57
54
  # a sampler to sample array elements from
58
- # @param [Boolean] non_empty
59
- # pass true to never produce empty arrays
60
- def list_of sampler, non_empty: false
61
- lower_bound = non_empty ? 1 : 0
62
- feed { sampler.first(rand(lower_bound..upper_bound)) }
55
+ def list_of sampler, size: 1..10
56
+ send_to(sampler, :first, size)
63
57
  end
64
58
 
65
- # Sampler of uniform fixed length arrays
59
+ # Sampler of uniform fixed size arrays
66
60
  #
67
61
  # **Rosetta stone** this sampler corresponds to QuickCheck's `vectorOf`.
68
62
  #
69
63
  # @return [Quick::Sampler]
70
- # a sampler that produces arrays of `length` of values sampled from `sampler`
71
- # @param [Integer] length
72
- # sample array length
64
+ # a sampler that produces arrays of `size` of values sampled from `sampler`
65
+ # @param [Integer] size
66
+ # sample array size
73
67
  # @param [Quick::Sampler] sampler
74
68
  # a sampler to sample array elements from
75
- def vector_of length, sampler
76
- feed { sampler.take(length).force }
69
+ def vector_of size, sampler
70
+ feed { sampler.take(size).force }
77
71
  end
78
72
 
79
73
  # Sampler of arbitrary nested structures made up of `Array`s, `Hash`es, `Quick::Sampler`s and
@@ -85,7 +85,7 @@ module Quick
85
85
  # @todo document character classes
86
86
  # @param [Integer, Range, Quick::Sampler<Integer>] size:
87
87
  # Length of the string to generate
88
- def string *classes, size: pick_from(1..10)
88
+ def string *classes, size: 1..10
89
89
  classes = [:printable] if classes.empty?
90
90
  repertoire = DSL::CharacterClass.expand(*classes)
91
91
  size = pick_from(size) if Range === size
@@ -13,7 +13,6 @@ module Quick
13
13
  # (Incidentally, {Fluidiom} instances for deeper nested sub-samplers get
14
14
  # leaked from compile at the moment)
15
15
  class DSL
16
- include Quick::Sampler::Config
17
16
  include SimpleValues
18
17
  include SimpleCombinators
19
18
 
@@ -44,7 +43,7 @@ module Quick
44
43
  # @yieldreturn [<Sample>] a sampled value
45
44
  #
46
45
  def feed &block
47
- Fluidiom.new(Base.new(block), config)
46
+ Fluidiom.new(Base.new(block))
48
47
  end
49
48
 
50
49
  private
@@ -1,5 +1,5 @@
1
1
  module Quick
2
2
  module Sampler
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quick-sampler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artem Baguinski
@@ -112,7 +112,6 @@ files:
112
112
  - lib/quick/sampler.rb
113
113
  - lib/quick/sampler/autoload.rb
114
114
  - lib/quick/sampler/base.rb
115
- - lib/quick/sampler/config.rb
116
115
  - lib/quick/sampler/dsl.rb
117
116
  - lib/quick/sampler/dsl/character_class.rb
118
117
  - lib/quick/sampler/dsl/fluidiom.rb
@@ -1,25 +0,0 @@
1
- require "active_support/configurable"
2
- require "active_support/concern"
3
-
4
- module Quick
5
- module Sampler
6
- module Config
7
- extend ActiveSupport::Concern
8
- include ActiveSupport::Configurable
9
-
10
- included do
11
- config_accessor(:max_iterations) { 1000 }
12
- config_accessor(:upper_bound) { 25 }
13
- end
14
-
15
- def config options = :none_given
16
- if options == :none_given
17
- super()
18
- else
19
- config.merge!(options)
20
- self
21
- end
22
- end
23
- end
24
- end
25
- end