Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement functor/fmap for the IFn interface

  • Loading branch information...
commit 4dd3e17e494b7b6f43cda045d3162b0f60df7599 1 parent 86be48c
@khinsen khinsen authored
View
8 src/main/clojure/clojure/algo/generic/functor.clj
@@ -37,3 +37,11 @@
(defmethod fmap clojure.lang.IPersistentSet
[f s]
(into (empty s) (map f s)))
+
+(defmethod fmap clojure.lang.IFn
+ [f fn]
+ (comp f fn))
+
+(prefer-method fmap clojure.lang.IPersistentVector clojure.lang.IFn)
+(prefer-method fmap clojure.lang.IPersistentMap clojure.lang.IFn)
+(prefer-method fmap clojure.lang.IPersistentSet clojure.lang.IFn)
View
6 src/test/clojure/clojure/algo/generic/test_functor.clj
@@ -21,6 +21,12 @@
(gf/fmap inc {:A 1 :B 2 :C 3}) {:A 2 :B 3 :C 4}
(gf/fmap inc #{1 2 3}) #{2 3 4}))
+; Test implementation for functions
+(deftest functions
+ (let [f (fn [x] (+ x x))
+ x [-1 0 1 2]]
+ (is (= (map (gf/fmap - f) x)
+ (map (comp - f) x)))))
; Define a multiset class. The representation is a map from values to counts.
(defrecord multiset [map])
Please sign in to comment.
Something went wrong with that request. Please try again.