Skip to content

clojure.core/mapcat

View this page on ClojureDocs

Type: function Added: Clojure 1.0 Examples: 9 Runnable: 7
([f] [f & colls])

Returns the result of applying concat to the result of applying map to f and colls. Thus function f should return a collection. Returns a transducer when no collections are provided

Examples

by nipra on
by devn on . May have evaluation errors.
by uvtc on . May have evaluation errors.
by uvtc on
by devth on
by Dimagog on
by buzzdan on
by humorless on
by sodesu99 on

Note by reborg

;; mapcat always evaluates the first 4 arguments.
(def a (mapcat range (map #(do (print ".") %) (into () (range 10)))))
;; ....

;; it can be solved avoiding 'apply' to handle varargs
(defn mapcat* [f & colls]
  (letfn [(step [colls]
            (lazy-seq
              (when-first [c colls]
                (concat c (step (rest colls))))))]
    (step (apply map f colls))))

(def a (mapcat* range (map #(do (print ".") %) (into () (range 10)))))
;; nothing prints

See also


Content from the matching ClojureDocs page, with authors credited on each contribution.