diff 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
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/contrib/test_contrib/accumulators/examples.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,93 @@
     1.4 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     1.5 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     1.6 +;;
     1.7 +;; Accumulator application examples
     1.8 +;;
     1.9 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    1.10 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    1.11 +
    1.12 +(ns
    1.13 +  #^{:author "Konrad Hinsen"
    1.14 +     :skip-wiki true
    1.15 +     :doc "Examples for using accumulators"}
    1.16 +  clojure.contrib.accumulators.examples
    1.17 +  (:use [clojure.contrib.accumulators
    1.18 +	 :only (combine add add-items
    1.19 +		empty-vector empty-list empty-queue empty-set empty-map
    1.20 +		empty-counter empty-counter-with-total
    1.21 +		empty-sum empty-product empty-maximum empty-minimum
    1.22 +		empty-min-max empty-mean-variance empty-string empty-tuple)]))
    1.23 +
    1.24 +; Vector accumulator: combine is concat, add is conj
    1.25 +(combine [:a :b] [:c :d] [:x :y])
    1.26 +(add [:a :b] :c)
    1.27 +(add-items empty-vector [:a :b :a])
    1.28 +
    1.29 +; List accumulator: combine is concat, add is conj
    1.30 +(combine '(:a :b) '(:c :d) '(:x :y))
    1.31 +(add '(:a :b) :c)
    1.32 +(add-items empty-list [:a :b :a])
    1.33 +
    1.34 +; Queue accumulator
    1.35 +(let [q1 (add-items empty-queue [:a :b :a])
    1.36 +      q2 (add-items empty-queue [:x :y])]
    1.37 +  (combine q1 q2))
    1.38 +
    1.39 +; Set accumulator: combine is union, add is conj
    1.40 +(combine #{:a :b} #{:c :d} #{:a :d})
    1.41 +(add #{:a :b} :c)
    1.42 +(add-items empty-set [:a :b :a])
    1.43 +
    1.44 +; Map accumulator: combine is merge, add is conj
    1.45 +(combine {:a 1} {:b 2 :c 3} {})
    1.46 +(add {:a 1} [:b 2])
    1.47 +(add-items empty-map [[:a 1] [:b 2] [:a 0]])
    1.48 +
    1.49 +; Counter accumulator
    1.50 +(let [c1 (add-items empty-counter [:a :b :a])
    1.51 +      c2 (add-items empty-counter [:x :y])]
    1.52 +  (combine c1 c2))
    1.53 +
    1.54 +; Counter-with-total accumulator
    1.55 +(let [c1 (add-items empty-counter-with-total [:a :b :a])
    1.56 +      c2 (add-items empty-counter-with-total [:x :y])]
    1.57 +  (combine c1 c2))
    1.58 +
    1.59 +; Sum accumulator: combine is addition
    1.60 +(let [s1 (add-items empty-sum [1 2 3])
    1.61 +      s2 (add-items empty-sum [-1 -2 -3])]
    1.62 +  (combine s1 s2))
    1.63 +
    1.64 +; Product accumulator: combine is multiplication
    1.65 +(let [p1 (add-items empty-product [2 3])
    1.66 +      p2 (add-items empty-product [(/ 1 2)])]
    1.67 +  (combine p1 p2))
    1.68 +
    1.69 +; Maximum accumulator: combine is max
    1.70 +(let [m1 (add-items empty-maximum [2 3])
    1.71 +      m2 (add-items empty-maximum [(/ 1 2)])]
    1.72 +  (combine m1 m2))
    1.73 +
    1.74 +; Minimum accumulator: combine is min
    1.75 +(let [m1 (add-items empty-minimum [2 3])
    1.76 +      m2 (add-items empty-minimum [(/ 1 2)])]
    1.77 +  (combine m1 m2))
    1.78 +
    1.79 +; Min-max accumulator: combination of minimum and maximum
    1.80 +(let [m1 (add-items empty-min-max [2 3])
    1.81 +      m2 (add-items empty-min-max [(/ 1 2)])]
    1.82 +  (combine m1 m2))
    1.83 +
    1.84 +; Mean-variance accumulator: sample mean and sample variance
    1.85 +(let [m1 (add-items empty-mean-variance [2 4])
    1.86 +      m2 (add-items empty-mean-variance [6])]
    1.87 +  (combine m1 m2))
    1.88 +
    1.89 +; String accumulator: combine is concatenation
    1.90 +(combine "a" "b" "c" "def")
    1.91 +(add "a" (char 44))
    1.92 +(add-items empty-string [(char 55) (char 56) (char 57)])
    1.93 +
    1.94 +; Accumulator tuples permit to update several accumulators in parallel
    1.95 +(let [pair (empty-tuple [empty-vector empty-string])]
    1.96 +  (add-items pair [[1 "a"] [2 "b"]]))