diff src/clojure/contrib/generic/collection.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/generic/collection.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,116 @@
     1.4 +;; Generic interfaces for collection-related functions
     1.5 +
     1.6 +;; by Konrad Hinsen
     1.7 +;; last updated May 5, 2009
     1.8 +
     1.9 +;; Copyright (c) Konrad Hinsen, 2009. All rights reserved.  The use
    1.10 +;; and distribution terms for this software are covered by the Eclipse
    1.11 +;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
    1.12 +;; which can be found in the file epl-v10.html at the root of this
    1.13 +;; distribution.  By using this software in any fashion, you are
    1.14 +;; agreeing to be bound by the terms of this license.  You must not
    1.15 +;; remove this notice, or any other, from this software.
    1.16 +
    1.17 +(ns
    1.18 +  ^{:author "Konrad Hinsen"
    1.19 +     :doc "Generic arithmetic interface
    1.20 +           This library defines generic versions of common
    1.21 +           collection-related functions as multimethods that can be
    1.22 +           defined for any type."}
    1.23 +  clojure.contrib.generic.collection
    1.24 +  (:refer-clojure :exclude [assoc conj dissoc empty get into seq]))
    1.25 +
    1.26 +;
    1.27 +; assoc
    1.28 +;
    1.29 +(defmulti assoc
    1.30 +  "Returns a new collection in which the values corresponding to the
    1.31 +   given keys are updated by the given values. Each type of collection
    1.32 +   can have specific restrictions on the possible keys."
    1.33 +   {:arglists '([coll & key-val-pairs])}
    1.34 +   (fn [coll & items] (type coll)))
    1.35 +
    1.36 +(defmethod assoc :default
    1.37 +  [map & key-val-pairs]
    1.38 +  (apply clojure.core/assoc map key-val-pairs))
    1.39 +
    1.40 +; assoc-in
    1.41 +
    1.42 +;
    1.43 +; conj
    1.44 +;
    1.45 +(defmulti conj
    1.46 +  "Returns a new collection resulting from adding all xs to coll."
    1.47 +   {:arglists '([coll & xs])}
    1.48 +  (fn [coll & xs] (type coll)))
    1.49 +
    1.50 +(defmethod conj :default
    1.51 +  [coll & xs]
    1.52 +  (apply clojure.core/conj coll xs))
    1.53 +
    1.54 +;
    1.55 +; diassoc
    1.56 +;
    1.57 +(defmulti dissoc
    1.58 +  "Returns a new collection in which the entries corresponding to the
    1.59 +   given keys are removed. Each type of collection can have specific
    1.60 +   restrictions on the possible keys."
    1.61 +   {:arglists '([coll & keys])}
    1.62 +   (fn [coll & keys] (type coll)))
    1.63 +
    1.64 +(defmethod dissoc :default
    1.65 +  [map & keys]
    1.66 +  (apply clojure.core/dissoc map keys))
    1.67 +
    1.68 +;
    1.69 +; empty
    1.70 +;
    1.71 +(defmulti empty
    1.72 +  "Returns an empty collection of the same kind as the argument"
    1.73 +   {:arglists '([coll])}
    1.74 +   type)
    1.75 +
    1.76 +(defmethod empty :default
    1.77 +  [coll]
    1.78 +  (clojure.core/empty coll))
    1.79 +
    1.80 +;
    1.81 +; get
    1.82 +;
    1.83 +(defmulti get
    1.84 +  "Returns the element of coll referred to by key. Each type of collection
    1.85 +   can have specific restrictions on the possible keys."
    1.86 +   {:arglists '([coll key] [coll key not-found])}
    1.87 +  (fn [coll & args] (type coll)))
    1.88 +
    1.89 +(defmethod get :default
    1.90 +  ([coll key]
    1.91 +     (clojure.core/get coll key))
    1.92 +  ([coll key not-found]
    1.93 +     (clojure.core/get coll key not-found)))
    1.94 +
    1.95 +;
    1.96 +; into
    1.97 +;
    1.98 +(defmulti into
    1.99 +  "Returns a new coll consisting of to-coll with all of the items of
   1.100 +  from-coll conjoined."
   1.101 +   {:arglists '([to from])}
   1.102 +  (fn [to from] (type to)))
   1.103 +
   1.104 +(declare seq)
   1.105 +(defmethod into :default
   1.106 +  [to from]
   1.107 +  (reduce conj to (seq from)))
   1.108 +
   1.109 +;
   1.110 +; seq
   1.111 +;
   1.112 +(defmulti seq
   1.113 +  "Returns a seq on the object s."
   1.114 +  {:arglists '([s])}
   1.115 +  type)
   1.116 +
   1.117 +(defmethod seq :default
   1.118 +  [s]
   1.119 +  (clojure.core/seq s))