annotate 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
rev   line source
rlm@10 1 ;; Generic interface for functors
rlm@10 2
rlm@10 3 ;; by Konrad Hinsen
rlm@10 4 ;; last updated May 3, 2009
rlm@10 5
rlm@10 6 ;; Copyright (c) Konrad Hinsen, 2009. All rights reserved. The use
rlm@10 7 ;; and distribution terms for this software are covered by the Eclipse
rlm@10 8 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
rlm@10 9 ;; which can be found in the file epl-v10.html at the root of this
rlm@10 10 ;; distribution. By using this software in any fashion, you are
rlm@10 11 ;; agreeing to be bound by the terms of this license. You must not
rlm@10 12 ;; remove this notice, or any other, from this software.
rlm@10 13
rlm@10 14 (ns
rlm@10 15 ^{:author "Konrad Hinsen"
rlm@10 16 :doc "Generic functor interface (fmap)"}
rlm@10 17 clojure.contrib.generic.functor)
rlm@10 18
rlm@10 19
rlm@10 20 (defmulti fmap
rlm@10 21 "Applies function f to each item in the data structure s and returns
rlm@10 22 a structure of the same kind."
rlm@10 23 {:arglists '([f s])}
rlm@10 24 (fn [f s] (type s)))
rlm@10 25
rlm@10 26 (defmethod fmap clojure.lang.IPersistentList
rlm@10 27 [f v]
rlm@10 28 (into (empty v) (map f v)))
rlm@10 29
rlm@10 30 (defmethod fmap clojure.lang.IPersistentVector
rlm@10 31 [f v]
rlm@10 32 (into (empty v) (map f v)))
rlm@10 33
rlm@10 34 (defmethod fmap clojure.lang.IPersistentMap
rlm@10 35 [f m]
rlm@10 36 (into (empty m) (for [[k v] m] [k (f v)])))
rlm@10 37
rlm@10 38 (defmethod fmap clojure.lang.IPersistentSet
rlm@10 39 [f s]
rlm@10 40 (into (empty s) (map f s)))