diff options
author | Stuart Sierra <mail@stuartsierra.com> | 2010-01-20 15:39:56 -0500 |
---|---|---|
committer | Stuart Sierra <mail@stuartsierra.com> | 2010-01-20 15:39:56 -0500 |
commit | 2ede388a9267d175bfaa7781ee9d57532eb4f20f (patch) | |
tree | bb42002af196405d7e25cc4e30b4c1c9de5c06d5 /src/main/clojure | |
parent | 1bc820d96048a6536706ff999e9892649b53c700 (diff) |
Move source files into Maven-style directory structure.
Diffstat (limited to 'src/main/clojure')
116 files changed, 19795 insertions, 0 deletions
diff --git a/src/main/clojure/clojure/contrib/accumulators.clj b/src/main/clojure/clojure/contrib/accumulators.clj new file mode 100644 index 00000000..dcd03dd1 --- /dev/null +++ b/src/main/clojure/clojure/contrib/accumulators.clj @@ -0,0 +1,324 @@ +;; Accumulators + +;; by Konrad Hinsen +;; last updated May 19, 2009 + +;; This module defines various accumulators (list, vector, map, +;; sum, product, counter, and combinations thereof) with a common +;; interface defined by the multimethods add and combine. +;; For each accumulator type, its empty value is defined in this module. +;; Applications typically use this as a starting value and add data +;; using the add multimethod. + +;; Copyright (c) Konrad Hinsen, 2009. All rights reserved. The use +;; and distribution terms for this software are covered by the Eclipse +;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) +;; which can be found in the file epl-v10.html at the root of this +;; distribution. By using this software in any fashion, you are +;; agreeing to be bound by the terms of this license. You must not +;; remove this notice, or any other, from this software. + +(ns + #^{:author "Konrad Hinsen" + :doc "A generic accumulator interface and implementations of various + accumulators."} + clojure.contrib.accumulators + (:refer-clojure :exclude (deftype)) + (:use [clojure.contrib.types :only (deftype)]) + (:use [clojure.contrib.def :only (defvar defvar- defmacro-)]) + (:require [clojure.contrib.generic.arithmetic :as ga])) + +(defmulti add + "Add item to the accumulator acc. The exact meaning of adding an + an item depends on the type of the accumulator." + {:arglists '([acc item])} + (fn [acc item] (type acc))) + +(defn add-items + "Add all elements of a collection coll to the accumulator acc." + [acc items] + (reduce add acc items)) + +(defmulti combine + "Combine the values of the accumulators acc1 and acc2 into a + single accumulator of the same type." + {:arglists '([& accs])} + (fn [& accs] (type (first accs)))) + +; +; An ::accumulator type tag is attached to tbe built-in types +; when used as accumulators, and new types are derived from it. +; Multimethods add and combine for ::accumulator sub-dispatch on class. +; We also define generic addition as the combine operation. +; +(let [meta-map {:type ::accumulator}] + (defn- with-acc-tag + [x] + (with-meta x meta-map))) + +(defmethod add ::accumulator + [a e] + ((get-method add (class a)) a e)) + +(defmethod combine ::accumulator + [& as] + (apply (get-method combine (class (first as))) as)) + +(defmethod ga/+ ::accumulator + [x y] + (combine x y)) + +; +; Vector accumulator +; +(defvar empty-vector (with-acc-tag []) + "An empty vector accumulator. Adding an item appends it at the end.") + +(defmethod combine clojure.lang.IPersistent |