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