annotate 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
rev   line source
rlm@10 1 ;; Generic interfaces for collection-related functions
rlm@10 2
rlm@10 3 ;; by Konrad Hinsen
rlm@10 4 ;; last updated May 5, 2009
rlm@10 5
rlm@10 6 ;; Copyright (c) Konrad Hinsen, 2009. All rights reserved. The use
rlm@10 7 ;; and distribution terms for this software are covered by the Eclipse
rlm@10 8 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
rlm@10 9 ;; which can be found in the file epl-v10.html at the root of this
rlm@10 10 ;; distribution. By using this software in any fashion, you are
rlm@10 11 ;; agreeing to be bound by the terms of this license. You must not
rlm@10 12 ;; remove this notice, or any other, from this software.
rlm@10 13
rlm@10 14 (ns
rlm@10 15 ^{:author "Konrad Hinsen"
rlm@10 16 :doc "Generic arithmetic interface
rlm@10 17 This library defines generic versions of common
rlm@10 18 collection-related functions as multimethods that can be
rlm@10 19 defined for any type."}
rlm@10 20 clojure.contrib.generic.collection
rlm@10 21 (:refer-clojure :exclude [assoc conj dissoc empty get into seq]))
rlm@10 22
rlm@10 23 ;
rlm@10 24 ; assoc
rlm@10 25 ;
rlm@10 26 (defmulti assoc
rlm@10 27 "Returns a new collection in which the values corresponding to the
rlm@10 28 given keys are updated by the given values. Each type of collection
rlm@10 29 can have specific restrictions on the possible keys."
rlm@10 30 {:arglists '([coll & key-val-pairs])}
rlm@10 31 (fn [coll & items] (type coll)))
rlm@10 32
rlm@10 33 (defmethod assoc :default
rlm@10 34 [map & key-val-pairs]
rlm@10 35 (apply clojure.core/assoc map key-val-pairs))
rlm@10 36
rlm@10 37 ; assoc-in
rlm@10 38
rlm@10 39 ;
rlm@10 40 ; conj
rlm@10 41 ;
rlm@10 42 (defmulti conj
rlm@10 43 "Returns a new collection resulting from adding all xs to coll."
rlm@10 44 {:arglists '([coll & xs])}
rlm@10 45 (fn [coll & xs] (type coll)))
rlm@10 46
rlm@10 47 (defmethod conj :default
rlm@10 48 [coll & xs]
rlm@10 49 (apply clojure.core/conj coll xs))
rlm@10 50
rlm@10 51 ;
rlm@10 52 ; diassoc
rlm@10 53 ;
rlm@10 54 (defmulti dissoc
rlm@10 55 "Returns a new collection in which the entries corresponding to the
rlm@10 56 given keys are removed. Each type of collection can have specific
rlm@10 57 restrictions on the possible keys."
rlm@10 58 {:arglists '([coll & keys])}
rlm@10 59 (fn [coll & keys] (type coll)))
rlm@10 60
rlm@10 61 (defmethod dissoc :default
rlm@10 62 [map & keys]
rlm@10 63 (apply clojure.core/dissoc map keys))
rlm@10 64
rlm@10 65 ;
rlm@10 66 ; empty
rlm@10 67 ;
rlm@10 68 (defmulti empty
rlm@10 69 "Returns an empty collection of the same kind as the argument"
rlm@10 70 {:arglists '([coll])}
rlm@10 71 type)
rlm@10 72
rlm@10 73 (defmethod empty :default
rlm@10 74 [coll]
rlm@10 75 (clojure.core/empty coll))
rlm@10 76
rlm@10 77 ;
rlm@10 78 ; get
rlm@10 79 ;
rlm@10 80 (defmulti get
rlm@10 81 "Returns the element of coll referred to by key. Each type of collection
rlm@10 82 can have specific restrictions on the possible keys."
rlm@10 83 {:arglists '([coll key] [coll key not-found])}
rlm@10 84 (fn [coll & args] (type coll)))
rlm@10 85
rlm@10 86 (defmethod get :default
rlm@10 87 ([coll key]
rlm@10 88 (clojure.core/get coll key))
rlm@10 89 ([coll key not-found]
rlm@10 90 (clojure.core/get coll key not-found)))
rlm@10 91
rlm@10 92 ;
rlm@10 93 ; into
rlm@10 94 ;
rlm@10 95 (defmulti into
rlm@10 96 "Returns a new coll consisting of to-coll with all of the items of
rlm@10 97 from-coll conjoined."
rlm@10 98 {:arglists '([to from])}
rlm@10 99 (fn [to from] (type to)))
rlm@10 100
rlm@10 101 (declare seq)
rlm@10 102 (defmethod into :default
rlm@10 103 [to from]
rlm@10 104 (reduce conj to (seq from)))
rlm@10 105
rlm@10 106 ;
rlm@10 107 ; seq
rlm@10 108 ;
rlm@10 109 (defmulti seq
rlm@10 110 "Returns a seq on the object s."
rlm@10 111 {:arglists '([s])}
rlm@10 112 type)
rlm@10 113
rlm@10 114 (defmethod seq :default
rlm@10 115 [s]
rlm@10 116 (clojure.core/seq s))