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 +4 -4
- data/lib/quick/sampler/base.rb +1 -1
- data/lib/quick/sampler/dsl/simple_combinators.rb +36 -2
- data/lib/quick/sampler/dsl/simple_values.rb +18 -0
- data/lib/quick/sampler/dsl.rb +7 -6
- data/lib/quick/sampler/version.rb +1 -1
- data/spec/quick/sampler/dsl/combine_spec.rb +11 -0
- data/spec/quick/sampler/dsl/merge_spec.rb +19 -0
- data/spec/quick/sampler/dsl/simple_values_spec.rb +22 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 35617377a366c985607e9bd8c955f2a51885625a
|
4
|
+
data.tar.gz: c2becb43c4867529b643533629c6c9256df079d6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d2a1fe1f914b0a12fa11e2eb81ffb97b986d3b5ffc6049d4bf435f7cdd02f280f66be459fd1a6b59bb5d726445952d4b89b02709489ea19b53ba7729bc51c2c
|
7
|
+
data.tar.gz: cf867a49b484f43542de4353fb47f1408f9e8dce55e1681b971f2bf57dc29523364f2a919b9ac38200b056b13c6ff7c9313136759feea72dd5e1e79e5b45aae9
|
data/lib/quick/sampler/base.rb
CHANGED
@@ -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
|
-
|
36
|
-
|
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
|
data/lib/quick/sampler/dsl.rb
CHANGED
@@ -35,15 +35,16 @@ module Quick
|
|
35
35
|
"Quick Sampler DSL"
|
36
36
|
end
|
37
37
|
|
38
|
-
#
|
38
|
+
# Sample an enumerable or block
|
39
39
|
#
|
40
|
-
#
|
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
|
-
|
46
|
-
Fluidiom.new(Base.new(
|
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
|
@@ -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
|
+
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-
|
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
|