• (fmap f s)
Applies function f to each item in the data structure s and returns
a structure of the same kind.

1 Example top

  • ;; 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}
Log in to add / edit an example.

See Also top

  • 0
    clojure.core/map

    Returns a lazy sequence consisting of the result of applying f to the

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/contrib/generic/functor.clj:20 top

(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)))
Vars in clojure.contrib.generic.functor/fmap: defmulti fn type
Used in 0 other vars

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!

Log in to add a comment.