Permalink
Browse files

Implement functor/fmap for the IFn interface

  • Loading branch information...
1 parent 86be48c commit 4dd3e17e494b7b6f43cda045d3162b0f60df7599 @khinsen khinsen committed Feb 5, 2014
@@ -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)
@@ -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])

0 comments on commit 4dd3e17

Please sign in to comment.