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