Mercurial > lasercutter
diff src/clojure/contrib/generic/functor.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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/clojure/contrib/generic/functor.clj Sat Aug 21 06:25:44 2010 -0400 1.3 @@ -0,0 +1,40 @@ 1.4 +;; Generic interface for functors 1.5 + 1.6 +;; by Konrad Hinsen 1.7 +;; last updated May 3, 2009 1.8 + 1.9 +;; Copyright (c) Konrad Hinsen, 2009. All rights reserved. The use 1.10 +;; and distribution terms for this software are covered by the Eclipse 1.11 +;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 1.12 +;; which can be found in the file epl-v10.html at the root of this 1.13 +;; distribution. By using this software in any fashion, you are 1.14 +;; agreeing to be bound by the terms of this license. You must not 1.15 +;; remove this notice, or any other, from this software. 1.16 + 1.17 +(ns 1.18 + ^{:author "Konrad Hinsen" 1.19 + :doc "Generic functor interface (fmap)"} 1.20 + clojure.contrib.generic.functor) 1.21 + 1.22 + 1.23 +(defmulti fmap 1.24 + "Applies function f to each item in the data structure s and returns 1.25 + a structure of the same kind." 1.26 + {:arglists '([f s])} 1.27 + (fn [f s] (type s))) 1.28 + 1.29 +(defmethod fmap clojure.lang.IPersistentList 1.30 + [f v] 1.31 + (into (empty v) (map f v))) 1.32 + 1.33 +(defmethod fmap clojure.lang.IPersistentVector 1.34 + [f v] 1.35 + (into (empty v) (map f v))) 1.36 + 1.37 +(defmethod fmap clojure.lang.IPersistentMap 1.38 + [f m] 1.39 + (into (empty m) (for [[k v] m] [k (f v)]))) 1.40 + 1.41 +(defmethod fmap clojure.lang.IPersistentSet 1.42 + [f s] 1.43 + (into (empty s) (map f s)))