rlm@10
|
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
rlm@10
|
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
rlm@10
|
3 ;;
|
rlm@10
|
4 ;; Accumulator application examples
|
rlm@10
|
5 ;;
|
rlm@10
|
6 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
rlm@10
|
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
rlm@10
|
8
|
rlm@10
|
9 (ns
|
rlm@10
|
10 #^{:author "Konrad Hinsen"
|
rlm@10
|
11 :skip-wiki true
|
rlm@10
|
12 :doc "Examples for using accumulators"}
|
rlm@10
|
13 clojure.contrib.accumulators.examples
|
rlm@10
|
14 (:use [clojure.contrib.accumulators
|
rlm@10
|
15 :only (combine add add-items
|
rlm@10
|
16 empty-vector empty-list empty-queue empty-set empty-map
|
rlm@10
|
17 empty-counter empty-counter-with-total
|
rlm@10
|
18 empty-sum empty-product empty-maximum empty-minimum
|
rlm@10
|
19 empty-min-max empty-mean-variance empty-string empty-tuple)]))
|
rlm@10
|
20
|
rlm@10
|
21 ; Vector accumulator: combine is concat, add is conj
|
rlm@10
|
22 (combine [:a :b] [:c :d] [:x :y])
|
rlm@10
|
23 (add [:a :b] :c)
|
rlm@10
|
24 (add-items empty-vector [:a :b :a])
|
rlm@10
|
25
|
rlm@10
|
26 ; List accumulator: combine is concat, add is conj
|
rlm@10
|
27 (combine '(:a :b) '(:c :d) '(:x :y))
|
rlm@10
|
28 (add '(:a :b) :c)
|
rlm@10
|
29 (add-items empty-list [:a :b :a])
|
rlm@10
|
30
|
rlm@10
|
31 ; Queue accumulator
|
rlm@10
|
32 (let [q1 (add-items empty-queue [:a :b :a])
|
rlm@10
|
33 q2 (add-items empty-queue [:x :y])]
|
rlm@10
|
34 (combine q1 q2))
|
rlm@10
|
35
|
rlm@10
|
36 ; Set accumulator: combine is union, add is conj
|
rlm@10
|
37 (combine #{:a :b} #{:c :d} #{:a :d})
|
rlm@10
|
38 (add #{:a :b} :c)
|
rlm@10
|
39 (add-items empty-set [:a :b :a])
|
rlm@10
|
40
|
rlm@10
|
41 ; Map accumulator: combine is merge, add is conj
|
rlm@10
|
42 (combine {:a 1} {:b 2 :c 3} {})
|
rlm@10
|
43 (add {:a 1} [:b 2])
|
rlm@10
|
44 (add-items empty-map [[:a 1] [:b 2] [:a 0]])
|
rlm@10
|
45
|
rlm@10
|
46 ; Counter accumulator
|
rlm@10
|
47 (let [c1 (add-items empty-counter [:a :b :a])
|
rlm@10
|
48 c2 (add-items empty-counter [:x :y])]
|
rlm@10
|
49 (combine c1 c2))
|
rlm@10
|
50
|
rlm@10
|
51 ; Counter-with-total accumulator
|
rlm@10
|
52 (let [c1 (add-items empty-counter-with-total [:a :b :a])
|
rlm@10
|
53 c2 (add-items empty-counter-with-total [:x :y])]
|
rlm@10
|
54 (combine c1 c2))
|
rlm@10
|
55
|
rlm@10
|
56 ; Sum accumulator: combine is addition
|
rlm@10
|
57 (let [s1 (add-items empty-sum [1 2 3])
|
rlm@10
|
58 s2 (add-items empty-sum [-1 -2 -3])]
|
rlm@10
|
59 (combine s1 s2))
|
rlm@10
|
60
|
rlm@10
|
61 ; Product accumulator: combine is multiplication
|
rlm@10
|
62 (let [p1 (add-items empty-product [2 3])
|
rlm@10
|
63 p2 (add-items empty-product [(/ 1 2)])]
|
rlm@10
|
64 (combine p1 p2))
|
rlm@10
|
65
|
rlm@10
|
66 ; Maximum accumulator: combine is max
|
rlm@10
|
67 (let [m1 (add-items empty-maximum [2 3])
|
rlm@10
|
68 m2 (add-items empty-maximum [(/ 1 2)])]
|
rlm@10
|
69 (combine m1 m2))
|
rlm@10
|
70
|
rlm@10
|
71 ; Minimum accumulator: combine is min
|
rlm@10
|
72 (let [m1 (add-items empty-minimum [2 3])
|
rlm@10
|
73 m2 (add-items empty-minimum [(/ 1 2)])]
|
rlm@10
|
74 (combine m1 m2))
|
rlm@10
|
75
|
rlm@10
|
76 ; Min-max accumulator: combination of minimum and maximum
|
rlm@10
|
77 (let [m1 (add-items empty-min-max [2 3])
|
rlm@10
|
78 m2 (add-items empty-min-max [(/ 1 2)])]
|
rlm@10
|
79 (combine m1 m2))
|
rlm@10
|
80
|
rlm@10
|
81 ; Mean-variance accumulator: sample mean and sample variance
|
rlm@10
|
82 (let [m1 (add-items empty-mean-variance [2 4])
|
rlm@10
|
83 m2 (add-items empty-mean-variance [6])]
|
rlm@10
|
84 (combine m1 m2))
|
rlm@10
|
85
|
rlm@10
|
86 ; String accumulator: combine is concatenation
|
rlm@10
|
87 (combine "a" "b" "c" "def")
|
rlm@10
|
88 (add "a" (char 44))
|
rlm@10
|
89 (add-items empty-string [(char 55) (char 56) (char 57)])
|
rlm@10
|
90
|
rlm@10
|
91 ; Accumulator tuples permit to update several accumulators in parallel
|
rlm@10
|
92 (let [pair (empty-tuple [empty-vector empty-string])]
|
rlm@10
|
93 (add-items pair [[1 "a"] [2 "b"]]))
|