File tree Expand file tree Collapse file tree 4 files changed +13
-15
lines changed Expand file tree Collapse file tree 4 files changed +13
-15
lines changed Original file line number Diff line number Diff line change 1
1
(ns examples.memoized-male-female )
2
2
3
+ ; Hofstadter's Male and Female Sequences from GEB
4
+ ; See http://en.wikipedia.org/wiki/Hofstadter_sequence
3
5
(declare m f )
4
6
(defn- m [n]
5
7
(if (= n 0 )
Original file line number Diff line number Diff line change 13
13
([] (.nextDouble *random*))
14
14
([n] (* n (.nextDouble *random*))))
15
15
16
- ; you can try this one to see that thread-local rand
17
- ; (above) is not the limiting factor
18
- ; (defn waste-time []
19
- ; (set! *random* (reduce + (take 100 (iterate inc 1)))))
20
-
21
- ; (defn tl-rand
22
- ; ([] (waste-time) 0.5)
23
- ; ([n] (* n (tl-rand))))
24
-
25
16
(defn random-point []
26
17
(let [random-coord (fn [] (dec (tl-rand 2 )))]
27
18
[(random-coord ) (random-coord )]))
39
30
40
31
(defmethod run-simulation [java.util.Map Number]
41
32
[results n]
42
- (println " Running on " (Thread/currentThread ))
43
33
(binding [*random* (java.util.Random. (next-seed ))]
44
34
(reduce (fn [{in-circle :in-circle total :total } point]
45
35
(struct sample-results
56
46
(defmethod guess-pi Number [n]
57
47
(guess-pi (run-simulation n)))
58
48
59
- ; START: pguess-pi
60
49
(defn parallel-guess-pi [agent-count trials]
61
50
(let [trials (quot trials agent-count)
62
51
agents (for [_ (range agent-count)] (agent trials))]
63
52
(doseq [a agents] (send a run-simulation))
64
53
(apply await agents)
65
54
(guess-pi (apply merge-with + (map deref agents)))))
66
- ; END: pguess-pi
67
55
56
+ ; runs an agent forever
57
+ (defn background-pi [iter-count]
58
+ (let [agt (agent {:in-circle 0 :total 0 })
59
+ continue (atom true )
60
+ iter (fn sim [a-val]
61
+ (when continue (send-off *agent* sim))
62
+ (run-simulation a-val iter-count))]
63
+ (send-off agt iter)
64
+ {:guesser agt :continue atom}))
Original file line number Diff line number Diff line change 83
83
84
84
(defn demo-xml-seq []
85
85
; START:xml-seq
86
- (for [x (xml-seq (parse (java.io.File. " examples/sequences/compositions.xml" )))
86
+ (for [x (xml-seq
87
+ (parse (java.io.File. " examples/sequences/compositions.xml" )))
87
88
:when (= :composition (:tag x))]
88
89
(:composer (:attrs x)))
89
90
; END:xml-seq
Original file line number Diff line number Diff line change 4
4
(def src " src" )
5
5
(def build " classes" )
6
6
7
- (default-target :compile )
8
-
9
7
(deftarget init
10
8
(tstamp )
11
9
(mkdir {:dir build}))
You can’t perform that action at this time.
0 commit comments