Mercurial > lasercutter
view 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 source
1 ;; Generic interfaces for collection-related functions3 ;; by Konrad Hinsen4 ;; last updated May 5, 20096 ;; Copyright (c) Konrad Hinsen, 2009. All rights reserved. The use7 ;; and distribution terms for this software are covered by the Eclipse8 ;; 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 this10 ;; distribution. By using this software in any fashion, you are11 ;; agreeing to be bound by the terms of this license. You must not12 ;; remove this notice, or any other, from this software.14 (ns15 ^{:author "Konrad Hinsen"16 :doc "Generic arithmetic interface17 This library defines generic versions of common18 collection-related functions as multimethods that can be19 defined for any type."}20 clojure.contrib.generic.collection21 (:refer-clojure :exclude [assoc conj dissoc empty get into seq]))23 ;24 ; assoc25 ;26 (defmulti assoc27 "Returns a new collection in which the values corresponding to the28 given keys are updated by the given values. Each type of collection29 can have specific restrictions on the possible keys."30 {:arglists '([coll & key-val-pairs])}31 (fn [coll & items] (type coll)))33 (defmethod assoc :default34 [map & key-val-pairs]35 (apply clojure.core/assoc map key-val-pairs))37 ; assoc-in39 ;40 ; conj41 ;42 (defmulti conj43 "Returns a new collection resulting from adding all xs to coll."44 {:arglists '([coll & xs])}45 (fn [coll & xs] (type coll)))47 (defmethod conj :default48 [coll & xs]49 (apply clojure.core/conj coll xs))51 ;52 ; diassoc53 ;54 (defmulti dissoc55 "Returns a new collection in which the entries corresponding to the56 given keys are removed. Each type of collection can have specific57 restrictions on the possible keys."58 {:arglists '([coll & keys])}59 (fn [coll & keys] (type coll)))61 (defmethod dissoc :default62 [map & keys]63 (apply clojure.core/dissoc map keys))65 ;66 ; empty67 ;68 (defmulti empty69 "Returns an empty collection of the same kind as the argument"70 {:arglists '([coll])}71 type)73 (defmethod empty :default74 [coll]75 (clojure.core/empty coll))77 ;78 ; get79 ;80 (defmulti get81 "Returns the element of coll referred to by key. Each type of collection82 can have specific restrictions on the possible keys."83 {:arglists '([coll key] [coll key not-found])}84 (fn [coll & args] (type coll)))86 (defmethod get :default87 ([coll key]88 (clojure.core/get coll key))89 ([coll key not-found]90 (clojure.core/get coll key not-found)))92 ;93 ; into94 ;95 (defmulti into96 "Returns a new coll consisting of to-coll with all of the items of97 from-coll conjoined."98 {:arglists '([to from])}99 (fn [to from] (type to)))101 (declare seq)102 (defmethod into :default103 [to from]104 (reduce conj to (seq from)))106 ;107 ; seq108 ;109 (defmulti seq110 "Returns a seq on the object s."111 {:arglists '([s])}112 type)114 (defmethod seq :default115 [s]116 (clojure.core/seq s))