quick-sampler 0.1.4 → 0.1.5

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
  SHA1:
3
- metadata.gz: 8ce420572de6ae77e74e2736e3501d5e9daf2953
4
- data.tar.gz: 1ab7c6e1c3c11277e105a47befad9fe974fe760c
3
+ metadata.gz: 35617377a366c985607e9bd8c955f2a51885625a
4
+ data.tar.gz: c2becb43c4867529b643533629c6c9256df079d6
5
5
  SHA512:
6
- metadata.gz: dbb04b8b15840fcc0e12885d3b82e8757696b381acbbb787c80ce9953d818849d6d977873dc666e6f9757e605d9e01fb66992ac3ac24c3d7f845e8c4399a754b
7
- data.tar.gz: 46d536fe6288ddfb8779b5b26131f4dfc813a61b6f9e76c24ccf37241e35b64aa008142f56eeab3b5c4fe7435adcc7da547f83bb3ad814dfc3518e20aa396d9c
6
+ metadata.gz: 8d2a1fe1f914b0a12fa11e2eb81ffb97b986d3b5ffc6049d4bf435f7cdd02f280f66be459fd1a6b59bb5d726445952d4b89b02709489ea19b53ba7729bc51c2c
7
+ data.tar.gz: cf867a49b484f43542de4353fb47f1408f9e8dce55e1681b971f2bf57dc29523364f2a919b9ac38200b056b13c6ff7c9313136759feea72dd5e1e79e5b45aae9
@@ -79,7 +79,7 @@ module Quick
79
79
  case source
80
80
  when Enumerator::Lazy
81
81
  source
82
- when Enumerator
82
+ when Enumerable
83
83
  source.lazy
84
84
  when ->(i) { i.respond_to? :call }
85
85
  Enumerator.new do |recipient|
@@ -32,8 +32,12 @@ module Quick
32
32
  def one_of_weighted expression_weights
33
33
  total_weight, expressions = expression_weights
34
34
  .reduce([0, {}]) { |(total_weight, expressions), (expression, weight)|
35
- total_weight += weight
36
- [total_weight, expressions.merge(total_weight => expression)]
35
+ if weight > 0
36
+ total_weight += weight
37
+ [total_weight, expressions.merge(total_weight => expression)]
38
+ else
39
+ [total_weight, expressions]
40
+ end
37
41
  }
38
42
 
39
43
  feed {
@@ -123,6 +127,36 @@ module Quick
123
127
  send_to(the_class, :new, *args)
124
128
  end
125
129
 
130
+ # Custom sampler combinator
131
+ #
132
+ # `combine` allows to implement a custom combination of samplers: the
133
+ # method expects individual samplers as arguments and the combination
134
+ # implementation as a block. The resulting sampler will sample each of
135
+ # the supplied sub-samplers, pass the sampled values to the block and emit
136
+ # block result as combined sample.
137
+ #
138
+ # @param [*Quick::Sampler] *samplers
139
+ # samplers to combine
140
+ # @return [Quick::Sampler]
141
+ # a combination sampler
142
+ # @yieldparam [*Anything] samples
143
+ # sampled values from each of the combined samplers
144
+ # @yieldreturn [Anything]
145
+ # combined sampled value
146
+ def combine *samplers, &block
147
+ list_like(*samplers).map(&block)
148
+ end
149
+
150
+ # Sampler of merged hashes
151
+ #
152
+ # @param [Hash, *Quick::Sampler<Hash>] *samplers
153
+ # `Hash`es or samplers emitting `Hash` values to merge
154
+ # @return [Quick::Sampler<Hash>]
155
+ # a sampler emitting merged hash
156
+ def merge *samplers
157
+ combine(*samplers) { |hashes| hashes.reduce(:merge) }
158
+ end
159
+
126
160
  private
127
161
 
128
162
  def recursive_sample value
@@ -92,6 +92,24 @@ module Quick
92
92
  send_to( send_to(repertoire, :sample, size), :join )
93
93
  end
94
94
 
95
+ # "Probability" sampler
96
+ #
97
+ # @return [Quick::Sampler<Float>]
98
+ # a sampler emitting a float in range `0.0..1.0`
99
+ def probability
100
+ pick_from(0..1.0)
101
+ end
102
+
103
+ # Weighted truth sampler
104
+ #
105
+ # @param [Float] weight
106
+ # probably of emitting `true`
107
+ # @return [Quick::Sampler<Boolean>]
108
+ # a sampler emitting `true` with probability `weight` or false with probability `(1.0 - weight)`
109
+ def weighted_truth weight
110
+ one_of_weighted true => weight, false => (1 - weight)
111
+ end
112
+
95
113
  end
96
114
  end
97
115
  end
@@ -35,15 +35,16 @@ module Quick
35
35
  "Quick Sampler DSL"
36
36
  end
37
37
 
38
- # Wraps a block into a lazy enumerator which will become sampler.
38
+ # Sample an enumerable or block
39
39
  #
40
- # I haven't decided yet if this is a private implementation detail
41
- # or a powerful albeit confusing DSL verb
40
+ # The block is ignored if `enum` parameter isn't `nil`
42
41
  #
42
+ # @param [Enumerable] enum
43
+ # an enumerable to sample values from
43
44
  # @yieldreturn [<Sample>] a sampled value
44
- #
45
- def feed &block
46
- Fluidiom.new(Base.new(block))
45
+ def feed enum = nil, &block
46
+ enum ||= block if block_given?
47
+ Fluidiom.new(Base.new(enum))
47
48
  end
48
49
 
49
50
  private
@@ -1,5 +1,5 @@
1
1
  module Quick
2
2
  module Sampler
3
- VERSION = "0.1.4"
3
+ VERSION = "0.1.5"
4
4
  end
5
5
  end
@@ -0,0 +1,11 @@
1
+ describe Quick::Sampler::DSL do
2
+ it { is_expected.to have_method :combine }
3
+
4
+ describe "#combine" do
5
+ let(:dsl) { described_class.new }
6
+ let(:english) { dsl.feed(["one", "two"]) }
7
+ let(:dutch) { dsl.feed(["één", "twee"]) }
8
+ subject(:combined) { dsl.combine(english, dutch) {|e,d| "#{e} = #{d}"}.force }
9
+ it { is_expected.to eq ["one = één", "two = twee"] }
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ describe Quick::Sampler::DSL do
2
+ it { is_expected.to have_method :merge }
3
+
4
+ describe "#merge" do
5
+ let(:dsl) { described_class.new }
6
+ let(:defaults_hash) { { prime: 7, negative: -42} }
7
+ let(:first_sampler) { dsl.feed( [{prime: 13}, {negative: -43}] ) }
8
+ let(:second_sampler) { dsl.feed( [{string: "one"}, {string: "two"}] ) }
9
+ subject(:merged) {
10
+ dsl.merge(defaults_hash, first_sampler, second_sampler).force
11
+ }
12
+ it {
13
+ is_expected.to contain_exactly(
14
+ { prime: 13, negative: -42, string: "one" },
15
+ { prime: 7, negative: -43, string: "two" },
16
+ )
17
+ }
18
+ end
19
+ end
@@ -32,4 +32,26 @@ describe Quick::Sampler::DSL do
32
32
  it { is_expected.to all match /^[a-z]{1,10}$/ }
33
33
  end
34
34
  end
35
+
36
+ describe "#probability" do
37
+ subject(:samples) { dsl.probability.first(5) }
38
+ it { is_expected.to all (be >= 0.0).and be <= 1.0 }
39
+ end
40
+
41
+ describe "#weighted_truth" do
42
+ describe "improbable truth" do
43
+ subject(:samples) { dsl.weighted_truth(0.0).first(5) }
44
+ it { is_expected.to all be false }
45
+ end
46
+
47
+ describe "certain truth" do
48
+ subject(:samples) { dsl.weighted_truth(1.0).first(5) }
49
+ it { is_expected.to all be true }
50
+ end
51
+
52
+ describe "maybe" do
53
+ subject(:samples) { dsl.weighted_truth(0.5).first(5) }
54
+ it { is_expected.to all be(true).or be(false) }
55
+ end
56
+ end
35
57
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quick-sampler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artem Baguinski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-07 00:00:00.000000000 Z
11
+ date: 2015-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -123,8 +123,10 @@ files:
123
123
  - lib/quick/sampler/version.rb
124
124
  - quick-sampler.gemspec
125
125
  - spec/quick/sampler/compile_spec.rb
126
+ - spec/quick/sampler/dsl/combine_spec.rb
126
127
  - spec/quick/sampler/dsl/hash_like_spec.rb
127
128
  - spec/quick/sampler/dsl/list_like_spec.rb
129
+ - spec/quick/sampler/dsl/merge_spec.rb
128
130
  - spec/quick/sampler/dsl/send_to_spec.rb
129
131
  - spec/quick/sampler/dsl/simple_values_spec.rb
130
132
  - spec/spec_helper.rb
@@ -154,8 +156,10 @@ specification_version: 4
154
156
  summary: Composable samplers of random data
155
157
  test_files:
156
158
  - spec/quick/sampler/compile_spec.rb
159
+ - spec/quick/sampler/dsl/combine_spec.rb
157
160
  - spec/quick/sampler/dsl/hash_like_spec.rb
158
161
  - spec/quick/sampler/dsl/list_like_spec.rb
162
+ - spec/quick/sampler/dsl/merge_spec.rb
159
163
  - spec/quick/sampler/dsl/send_to_spec.rb
160
164
  - spec/quick/sampler/dsl/simple_values_spec.rb
161
165
  - spec/spec_helper.rb