Skip to content

Commit da00e20

Browse files
committed
- exposing random source
so that external functions can tap in
1 parent 5666d8f commit da00e20

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

Common/RuleCandidateSelector.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ private extension MutableCollection {
4040
guard d != 0 else { continue }
4141
let i = index(firstUnshuffled, offsetBy: d)
4242
swapAt(firstUnshuffled, i)
43-
44-
4543
}
4644
}
4745
}

Common/Tracery.Eval.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ extension Tracery {
138138
contextStack.contexts[top].args.removeAll()
139139

140140
case let .any(values, selector, mods):
141-
let choice = values[selector.pick(count: values.count, randomSource: randomSource)]
141+
let choice = values[selector.pick(count: values.count, randomSource: randomSource_)]
142142
try applyMods(nodes: choice.nodes, mods: mods)
143143
// try pushContext(choice.nodes, affectsEvaluationLevel: false)
144144
trace("🎲 picked \(choice.nodes)")
@@ -272,7 +272,7 @@ extension Tracery {
272272

273273
func selectCandidate(_ mapping: RuleMapping, runTime: Bool) {
274274

275-
let index = mapping.selector.pick(count: mapping.candidates.count, randomSource: randomSource)
275+
let index = mapping.selector.pick(count: mapping.candidates.count, randomSource: randomSource_)
276276
guard index >= 0 && index < mapping.candidates.count else {
277277
state = .noExpansion(reason: "no candidates found")
278278
return
@@ -290,7 +290,7 @@ extension Tracery {
290290
state = .apply([.text("")])
291291
}
292292
else if let mapping = tagStorage.get(name: name) {
293-
let i = mapping.selector.pick(count: mapping.candidates.count, randomSource: randomSource)
293+
let i = mapping.selector.pick(count: mapping.candidates.count, randomSource: randomSource_)
294294
let value = mapping.candidates[i]
295295
trace("📗 get tag[\(name)] --> \(value)")
296296
state = .apply([.text(value)])

Common/Tracery.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@ public class Tracery {
4242
var mods: [String: (String,[String])->String]
4343
var tagStorage: TagStorage
4444
var contextStack: ContextStack
45-
var randomSource: RandomSource = FallbackRandomSource.shared
46-
45+
var randomSource_: RandomSource = FallbackRandomSource.shared
46+
47+
// wonky workaround for getting readonly access to randomSource
48+
public var randomSource: RandomSource {
49+
return randomSource_
50+
}
4751
public var ruleNames: [String] { return ruleSet.keys.map { $0 } }
4852
public var modifierNames: [String] { return mods.keys.map { $0 } }
4953

@@ -80,12 +84,12 @@ public class Tracery {
8084
info("tracery ready")
8185
}
8286

83-
func setSeed(_ seed:Int) {
87+
public func setSeed(_ seed:Int) {
8488
var s = seed
8589
let data = withUnsafePointer(to: &s) {
8690
Data(bytes: UnsafePointer($0), count: MemoryLayout.size(ofValue: seed))
8791
}
88-
randomSource = DeterministicRandomSource( seed: data )
92+
randomSource_ = DeterministicRandomSource( seed: data )
8993
}
9094

9195
func createRuleCandidate(rule:String, text: String) -> RuleCandidate? {
@@ -102,7 +106,7 @@ public class Tracery {
102106
return nil
103107
}
104108
}
105-
109+
106110
public func add(modifier: String, transform: @escaping (String)->String) {
107111
if mods[modifier] != nil {
108112
warn("overwriting modifier '\(modifier)'")

0 commit comments

Comments
 (0)