File tree Expand file tree Collapse file tree 2 files changed +13
-11
lines changed Expand file tree Collapse file tree 2 files changed +13
-11
lines changed Original file line number Diff line number Diff line change 23
23
(require pyherc.macros)
24
24
(import random)
25
25
26
- (defn chain-factory [start-elements elements]
26
+ (defn chain-factory [start-elements elements continue -fn ]
27
27
" create factory function that can create markov chain instances"
28
28
(fn []
29
29
" create generator for chain"
30
30
(defn select-next-element [elements-list]
31
31
" select element"
32
- (let [[high (max (list-comp upper [#t(element lower upper) elements-list]))]
32
+ (let [[high (max (list-comp upper [#t (element lower upper) elements-list]))]
33
33
[value (.randint random 0 high)]
34
- [matches (list-comp element [#t(element lower upper) elements-list]
34
+ [matches (list-comp element [#t (element lower upper) elements-list]
35
35
(> lower value upper))]]
36
36
(if matches
37
37
(first (.choice random matches))
42
42
(yield current-element)
43
43
(while running
44
44
(setv next-elements (get elements current-element))
45
- (if next-elements
46
- (setv current-element (select-next-element next-elements))
47
- (setv running false ))
45
+ (setv current-element (select-next-element next-elements))
46
+ (setv running (continue -fn current-element))
48
47
(when (not running) (break ))
49
48
(yield current-element))))
50
-
Original file line number Diff line number Diff line change 30
30
31
31
(background infinite-foo-chain
32
32
[factory (chain-factory :start-elements [#t("foo" 1 100 )]
33
- :elements {" foo" [#t("foo" 1 100 )]})]
33
+ :elements {" foo" [#t("foo" 1 100 )]}
34
+ :continue-fn (fn [item] true))]
34
35
[chain (factory )])
35
36
36
37
(fact " first element of chain can be read"
45
46
46
47
(background one-element-chain
47
48
[factory (chain-factory :start-elements [#t("foo" 1 100 )]
48
- :elements {" foo" []})]
49
+ :elements {" foo" [#t(nil 1 100 )]}
50
+ :continue-fn (fn [item] (not (is item nil))))]
49
51
[chain (factory )])
50
52
51
53
(fact " one element markov chain contains only one element"
58
60
(background flip-flop-chain
59
61
[factory (chain-factory :start-elements [#t("flip" 1 100 )]
60
62
:elements {" flip" [#t("flop" 1 100 )]
61
- " flop" [#t("flip" 1 100 )]})]
63
+ " flop" [#t("flip" 1 100 )]}
64
+ :continue-fn (fn [item] true))]
62
65
[chain (factory )])
63
66
64
67
(fact " markov chain can switch between states"
76
79
" bar" [#t("foo" 1 50 )
77
80
#t("baz" 51 100 )]
78
81
" baz" [#t("foo" 1 50 )
79
- #t("bar" 51 100 )]})]
82
+ #t("bar" 51 100 )]}
83
+ :continue-fn (fn [item] true))]
80
84
[chain (factory )])
81
85
82
86
(fact " markov chain can have multiple transitions from single element"
You can’t perform that action at this time.
0 commit comments