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