diff options
author | Konrad Hinsen <konrad.hinsen@laposte.net> | 2009-03-18 09:16:30 +0000 |
---|---|---|
committer | Konrad Hinsen <konrad.hinsen@laposte.net> | 2009-03-18 09:16:30 +0000 |
commit | 334ad56890518ddd27c1327a25c91f8db873e417 (patch) | |
tree | a38f17f40b553de7ffc608928c7a9b2f5203b1b8 | |
parent | 832224974863b538b509715607c24a38a236554f (diff) |
generic: new module functor
-rw-r--r-- | build.xml | 1 | ||||
-rw-r--r-- | src/clojure/contrib/gen_html_docs.clj | 1 | ||||
-rw-r--r-- | src/clojure/contrib/generic/collection.clj | 46 | ||||
-rw-r--r-- | src/clojure/contrib/generic/functor.clj | 38 | ||||
-rw-r--r-- | src/clojure/contrib/load_all.clj | 1 | ||||
-rw-r--r-- | src/clojure/contrib/types/examples.clj | 11 |
6 files changed, 57 insertions, 41 deletions
@@ -86,6 +86,7 @@ <arg value="clojure.contrib.fcase"/> <arg value="clojure.contrib.generic.arithmetic"/> <arg value="clojure.contrib.generic.collection"/> + <arg value="clojure.contrib.generic.functor"/> <arg value="clojure.contrib.generic.math-functions"/> <arg value="clojure.contrib.import-static"/> <arg value="clojure.contrib.javadoc.browse"/> diff --git a/src/clojure/contrib/gen_html_docs.clj b/src/clojure/contrib/gen_html_docs.clj index c00d3513..a008a75a 100644 --- a/src/clojure/contrib/gen_html_docs.clj +++ b/src/clojure/contrib/gen_html_docs.clj @@ -484,6 +484,7 @@ emits the generated HTML to the path named by path." 'clojure.contrib.fcase 'clojure.contrib.generic.arithmetic 'clojure.contrib.generic.collection + 'clojure.contrib.generic.functor 'clojure.contrib.generic.math-functions 'clojure.contrib.import-static 'clojure.contrib.javadoc 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))) diff --git a/src/clojure/contrib/load_all.clj b/src/clojure/contrib/load_all.clj index 9890803d..6cbdd872 100644 --- a/src/clojure/contrib/load_all.clj +++ b/src/clojure/contrib/load_all.clj @@ -44,6 +44,7 @@ except fcase generic.arithmetic generic.collection +generic.functor generic.math-functions import-static javadoc.browse diff --git a/src/clojure/contrib/types/examples.clj b/src/clojure/contrib/types/examples.clj index 214f7e1c..8c56b1c5 100644 --- a/src/clojure/contrib/types/examples.clj +++ b/src/clojure/contrib/types/examples.clj @@ -9,7 +9,8 @@ (ns clojure.contrib.types.examples (:use [clojure.contrib.types :only (deftype defadt match)]) - (:require [clojure.contrib.generic.collection :as gc])) + (:require [clojure.contrib.generic.collection :as gc]) + (:require [clojure.contrib.generic.functor :as gf])) ; ; Multisets implemented as maps to integers @@ -74,15 +75,15 @@ (depth empty-tree) (depth a-tree) -; Algebraic data types with multimethods: map on a tree -(defmethod gc/map ::tree +; Algebraic data types with multimethods: fmap on a tree +(defmethod gf/fmap ::tree [f t] (match t empty-tree empty-tree (leaf v) (leaf (f v)) - (node l r) (node (gc/map f l) (gc/map f r)))) + (node l r) (node (gf/fmap f l) (gf/fmap f r)))) -(gc/map str a-tree) +(gf/fmap str a-tree) ; ; Nonsense examples to illustrate all the features of match |