Applies function f to each item in the data structure s and returns
a structure of the same kind.
;; when used with a list, note the reversed order of the output:
user=> (fmap inc '(1 2 3))
(4 3 2)
user=> (fmap inc [1 2 3])
[2 3 4]
user=> (fmap inc #{1 2 3})
#{2 3 4}
;; when used with a map, returns a map where the values have been transformed by f:
user=> (fmap inc {:a 1, :b 2, :c 3})
{:a 2, :b 3, :c 4}
(defmulti fmap
"Applies function f to each item in the data structure s and returns
a structure of the same kind."
{:arglists '([f s])}
(fn [f s] (type s)))
Comments top
4 comment(s) for fmap.
someone please leave examples of this!
(defmulti fmap "Applies function f to each item in the data structure s and returns a structure of the same kind." {:arglists '([f s])} (fn [f s] (type s)))
(defmethod fmap clojure.lang.IPersistentList [f v] (into (empty v) (map f v)))
(defmethod fmap clojure.lang.IPersistentVector [f v] (into (empty v) (map f v)))
(defmethod fmap clojure.lang.IPersistentMap [f m] (into (empty m) (for [[k v] m] [k (f v)])))
(defmethod fmap clojure.lang.IPersistentSet [f s] (into (empty s) (map f s)))
The namespace for this is now clojure.algo.generic.functor (in org.clojure:algo.generic:0.1.1).
this is so damn awesome!