calyx 0.12.0 → 0.12.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 19046d53109e4dfe958d0e9e4fd3a3ff2d446109
4
- data.tar.gz: 4a41b45d945bcb44ea7359db7398d0bdb6c366e8
3
+ metadata.gz: 5ef3b96315fd2ed6eb9352e21a11a4fe650c06cb
4
+ data.tar.gz: d5c9cb27ee6626cdae2fe30ceef5c6bdb369e260
5
5
  SHA512:
6
- metadata.gz: 38527d602bf96335cef8c504612c0d4bfb1fbf17dd459420123ceeb2c088207921e0d68580ec1777cf822476ab4e3a935f8f2b02d2d7834642806a3593f4170c
7
- data.tar.gz: e49e3d348caaec3290e6412317f31e29054f4dbecce4dbf35c264c2a952fc36c442e1adeb7af2d6444ca7429f82d59f1b2bf504f15dc6967641a383fa09ca18c
6
+ metadata.gz: 97709bec536e1f1ea619ec7f882184b4e4ec45c32b29371cd1c2ba30a88bbbcb0c637cce3d52ac5ebc6c37d642c8c51ff9fb47785a8c9775ffee0490d91bcdcd
7
+ data.tar.gz: 927d4e0ee0380e58299b97f6cffaae351575fbd15fa99e446c48c7a16dff9b90055625460862a533a02476748809aeb9840ce2b872549d0917f19b618b7ccc46
@@ -10,7 +10,6 @@ tiny_woodland = Calyx::Grammar.new do
10
10
  trees "🌲", "🌳"
11
11
  foliage "🌿", "🌱"
12
12
  flowers "🌷", "🌻", "🌼"
13
- creatures "🐗", "🐻", "🐝", "🐦"
14
13
  br "\n"
15
14
  end
16
15
 
@@ -98,12 +98,12 @@ module Calyx
98
98
  # Grammar rules can be constructed on the fly when the passed-in block is
99
99
  # evaluated.
100
100
  #
101
- # @param [Number, Random] seed
102
- def initialize(seed=Random.new, &block)
103
- if seed.is_a?(Numeric)
104
- @rng = Random.new(seed)
101
+ # @param [Numeric, Random] random
102
+ def initialize(random=Random.new, &block)
103
+ if random.is_a?(Numeric)
104
+ @random = Random.new(random)
105
105
  else
106
- @rng = seed
106
+ @random = random
107
107
  end
108
108
 
109
109
  if block_given?
@@ -123,7 +123,7 @@ module Calyx
123
123
  def generate(*args)
124
124
  start_symbol, rules_map = map_default_args(*args)
125
125
 
126
- @registry.evaluate(start_symbol, @rng, rules_map).flatten.reject do |obj|
126
+ @registry.evaluate(start_symbol, @random, rules_map).flatten.reject do |obj|
127
127
  obj.is_a?(Symbol)
128
128
  end.join(''.freeze)
129
129
  end
@@ -137,7 +137,7 @@ module Calyx
137
137
  def evaluate(*args)
138
138
  start_symbol, rules_map = map_default_args(*args)
139
139
 
140
- @registry.evaluate(start_symbol, @rng, rules_map)
140
+ @registry.evaluate(start_symbol, @random, rules_map)
141
141
  end
142
142
 
143
143
  private
@@ -34,9 +34,10 @@ module Calyx
34
34
 
35
35
  # Evaluate the choice by randomly picking one of its possible options.
36
36
  #
37
+ # @param [Random] random
37
38
  # @return [Array]
38
- def evaluate(rng)
39
- [:choice, @collection.sample(random: rng).evaluate(rng)]
39
+ def evaluate(random)
40
+ [:choice, @collection.sample(random: random).evaluate(random)]
40
41
  end
41
42
  end
42
43
  end
@@ -50,10 +50,11 @@ module Calyx
50
50
  # Evaluate all the child nodes of this node and concatenate them together
51
51
  # into a single result.
52
52
  #
53
+ # @param [Random] random
53
54
  # @return [Array]
54
- def evaluate(rng)
55
+ def evaluate(random)
55
56
  concat = @expansion.reduce([]) do |exp, atom|
56
- exp << atom.evaluate(rng)
57
+ exp << atom.evaluate(random)
57
58
  end
58
59
 
59
60
  [:concat, concat]
@@ -17,9 +17,10 @@ module Calyx
17
17
  # terminal string, then passing it through the given modifier chain and
18
18
  # returning the transformed result.
19
19
  #
20
+ # @param [Random] random
20
21
  # @return [Array]
21
- def evaluate(rng)
22
- terminal = @production.evaluate(rng).flatten.reject { |o| o.is_a?(Symbol) }.join(''.freeze)
22
+ def evaluate(random)
23
+ terminal = @production.evaluate(random).flatten.reject { |o| o.is_a?(Symbol) }.join(''.freeze)
23
24
  expression = @methods.reduce(terminal) do |value, method|
24
25
  @registry.transform(method, value)
25
26
  end
@@ -17,8 +17,9 @@ module Calyx
17
17
 
18
18
  # Evaluate the memo, using the registry to handle the expansion.
19
19
  #
20
+ # @param [Random] random
20
21
  # @return [Array]
21
- def evaluate(rng)
22
+ def evaluate(random)
22
23
  [@symbol, @registry.memoize_expansion(@symbol)]
23
24
  end
24
25
  end
@@ -15,9 +15,10 @@ module Calyx
15
15
 
16
16
  # Evaluate the non-terminal, using the registry to handle the expansion.
17
17
  #
18
+ # @param [Random] random
18
19
  # @return [Array]
19
- def evaluate(rng)
20
- [@symbol, @registry.expand(@symbol).evaluate(rng)]
20
+ def evaluate(random)
21
+ [@symbol, @registry.expand(@symbol).evaluate(random)]
21
22
  end
22
23
  end
23
24
  end
@@ -11,8 +11,9 @@ module Calyx
11
11
 
12
12
  # Evaluate the terminal by returning its identity directly.
13
13
  #
14
+ # @param [Random] random
14
15
  # @return [Array]
15
- def evaluate(rng)
16
+ def evaluate(random)
16
17
  [:atom, @atom]
17
18
  end
18
19
  end
@@ -30,7 +30,6 @@ module Calyx
30
30
  # Initialize a new choice with a list of child nodes.
31
31
  #
32
32
  # @param [Array] collection
33
- # @param [Random] rng
34
33
  def initialize(collection)
35
34
  @collection = collection
36
35
  end
@@ -41,13 +40,14 @@ module Calyx
41
40
  # The method for selecting weighted probabilities is based on a snippet
42
41
  # of code recommended in the Ruby standard library documentation.
43
42
  #
43
+ # @param [Random] random
44
44
  # @return [Array]
45
- def evaluate(rng)
45
+ def evaluate(random)
46
46
  choice = @collection.max_by do |_, weight|
47
- rng.rand ** (1.0 / weight)
47
+ random.rand ** (1.0 / weight)
48
48
  end.first
49
49
 
50
- [:weighted_choice, choice.evaluate(rng)]
50
+ [:weighted_choice, choice.evaluate(random)]
51
51
  end
52
52
  end
53
53
  end
@@ -1,7 +1,7 @@
1
1
  module Calyx
2
2
  # Lookup table of all the available rules in the grammar.
3
3
  class Registry
4
- attr_reader :rng, :rules, :transforms, :modifiers
4
+ attr_reader :rules, :transforms, :modifiers
5
5
 
6
6
  # Construct an empty registry.
7
7
  def initialize
@@ -78,7 +78,7 @@ module Calyx
78
78
  #
79
79
  # @param [Symbol] symbol
80
80
  def memoize_expansion(symbol)
81
- memos[symbol] ||= expand(symbol).evaluate(@rng)
81
+ memos[symbol] ||= expand(symbol).evaluate(@random)
82
82
  end
83
83
 
84
84
  # Merges the given registry instance with the target registry.
@@ -91,16 +91,19 @@ module Calyx
91
91
  @rules = rules.merge(registry.rules)
92
92
  end
93
93
 
94
+
95
+
94
96
  # Evaluates the grammar defined in this registry, combining it with rules
95
97
  # from the passed in context.
96
98
  #
97
99
  # Produces a syntax tree of nested list nodes.
98
100
  #
99
101
  # @param [Symbol] start_symbol
102
+ # @param [Random] random
100
103
  # @param [Hash] rules_map
101
104
  # @return [Array]
102
- def evaluate(start_symbol=:start, rng=Random.new, rules_map={})
103
- reset_evaluation_context(rng)
105
+ def evaluate(start_symbol=:start, random=Random.new, rules_map={})
106
+ reset_evaluation_context(random)
104
107
 
105
108
  rules_map.each do |key, value|
106
109
  if rules.key?(key.to_sym)
@@ -117,7 +120,7 @@ module Calyx
117
120
  expansion = expand(start_symbol)
118
121
 
119
122
  if expansion.respond_to?(:evaluate)
120
- [start_symbol, expansion.evaluate(rng)]
123
+ [start_symbol, expansion.evaluate(random)]
121
124
  else
122
125
  raise Errors::MissingRule.new(start_symbol)
123
126
  end
@@ -125,10 +128,10 @@ module Calyx
125
128
 
126
129
  private
127
130
 
128
- attr_reader :rng, :memos, :context
131
+ attr_reader :random, :memos, :context
129
132
 
130
- def reset_evaluation_context(rng)
131
- @rng = rng
133
+ def reset_evaluation_context(random)
134
+ @random = random
132
135
  @context = {}
133
136
  @memos = {}
134
137
  end
@@ -1,3 +1,3 @@
1
1
  module Calyx
2
- VERSION = '0.12.0'.freeze
2
+ VERSION = '0.12.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: calyx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.12.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Rickerby
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-11 00:00:00.000000000 Z
11
+ date: 2016-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler