ClojureDocs

Nav

Namespaces

cat

clojure.core

Available since 1.7 (source)
  • (cat rf)
A transducer which concatenates the contents of each input, which must be a
collection, into the reduction.
4 Examples
;; cat is handy for untangling nested collections when using transducers

(into [] (comp cat cat (map inc)) [[[1] [2]] [[3] [4]]])
;; => [2 3 4 5]
;; Remove the need of (mapcat identity coll) idiom:
(def rota (sequence cat (repeat ["tom" "nick" "jane"])))
(nth rota 7) ; who's up next week?
;; nick

;; Although `cycle` would have done just fine here
;; combining map-indexed with cat for a "mapcat-indexed"
(into []
  (comp
    (map-indexed (fn [index val]
                   (repeat index val)))
    cat)
  (range 5))
;; => [1 2 2 3 3 3 4 4 4 4]
;;; a concise and performant way to concat into a vector

(into [] cat [[1 2 3] [4] [5 6 7]])
;; => [1 2 3 4 5 6 7]

(defn concatv
  "Concatenate `xs` and return the result as a vector."
  [& xs]
  (into [] cat xs))

(concatv '(1 2) [3] [4 5])
;; => [1 2 3 4 5]

;;; this xducer form is equivalent to the `(vec (concat...` pattern
(let [x [1 2 3]
      y 4
      z [5 6 7]]
  (= 
    (into [] cat [x [y] z])
    (vec (concat x [y] z))))

;; => true
See Also

A high-performance combining fn that yields the catenation of the reduced values. The result is re...

Added by achesnais

Equivalent to (fold cat append! coll)

Added by achesnais

Returns a new coll consisting of to-coll with all of the items of from-coll conjoined. A transduce...

Added by mars0i

Returns a lazy seq representing the concatenation of the elements in the supplied colls.

Added by mars0i
0 Notes
No notes for cat