aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib/generic
diff options
context:
space:
mode:
Diffstat (limited to 'src/clojure/contrib/generic')
-rw-r--r--src/clojure/contrib/generic/collection.clj46
-rw-r--r--src/clojure/contrib/generic/functor.clj38
2 files changed, 48 insertions, 36 deletions
diff --git a/src/clojure/contrib/generic/collection.clj b/src/clojure/contrib/generic/collection.clj
index 53e316a3..7d521804 100644
--- a/src/clojure/contrib/generic/collection.clj
+++ b/src/clojure/contrib/generic/collection.clj
@@ -1,7 +1,7 @@
;; Generic interfaces for collection-related functions
;; by Konrad Hinsen
-;; last updated March 16, 2009
+;; last updated March 18, 2009
;; Copyright (c) Konrad Hinsen, 2009. All rights reserved. The use
;; and distribution terms for this software are covered by the Eclipse
@@ -18,8 +18,8 @@
with future release.
This library defines generic versions of common collection-related functions
- such as map or conj as multimethods that can be defined for any type."
- (:refer-clojure :exclude [assoc conj dissoc empty get into map seq]))
+ as multimethods that can be defined for any type."
+ (:refer-clojure :exclude [assoc conj dissoc empty get into seq]))
;
; assoc
@@ -90,42 +90,16 @@
;
; into
;
-; This is a literal copy of into from clojure.core, but it is
-; evaluated with the functions from this namespace here!
-(declare seq)
-(defn into
+(defmulti into
"Returns a new coll consisting of to-coll with all of the items of
from-coll conjoined."
- [to from]
- (let [ret to items (seq from)]
- (if items
- (recur (conj ret (first items)) (next items))
- ret)))
+ {:arglists '([to from])}
+ (fn [to from] (type to)))
-;
-; map
-;
-(defmulti map
- "Applies function f to each element of coll and returns a collection
- of the same kind as coll."
- {:arglists '([f coll])}
- (fn [f coll] (type coll)))
-
-(defmethod map clojure.lang.ISeq
- [f coll]
- (clojure.core/map f coll))
-
-(defmethod map clojure.lang.IPersistentVector
- [f v]
- (clojure.core/into (clojure.core/empty v) (clojure.core/map f v)))
-
-(defmethod map clojure.lang.IPersistentMap
- [f m]
- (clojure.core/into (clojure.core/empty m) (for [[k v] m] [k (f v)])))
-
-(defmethod map clojure.lang.IPersistentSet
- [f s]
- (clojure.core/into (clojure.core/empty s) (clojure.core/map f s)))
+(declare seq)
+(defmethod into :default
+ [to from]
+ (reduce conj to (seq from)))
;
; seq
diff --git a/src/clojure/contrib/generic/functor.clj b/src/clojure/contrib/generic/functor.clj
new file mode 100644
index 00000000..1198c510
--- /dev/null
+++ b/src/clojure/contrib/generic/functor.clj
@@ -0,0 +1,38 @@
+;; Generic interface for functors
+
+;; by Konrad Hinsen
+;; last updated March 18, 2009
+
+;; 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 clojure.contrib.generic.functor
+ "Generic functor interface")
+
+
+(defmulti fmap
+ "Applies function f to each item in the data structure s and returns
+ a structure of the same kind."
+ {:arglists '([f s])}
+ (fn [f s] (type s)))
+
+(defmethod fmap clojure.lang.ISeq
+ [f s]
+ (map f s))
+
+(defmethod fmap clojure.lang.IPersistentVector
+ [f v]
+ (into (empty v) (map f v)))
+
+(defmethod fmap clojure.lang.IPersistentMap
+ [f m]
+ (into (empty m) (for [[k v] m] [k (f v)])))
+
+(defmethod fmap clojure.lang.IPersistentSet
+ [f s]
+ (into (empty s) (map f s)))