ClojureDocs

Nav

Namespaces

concat

clojure.core

Available since 1.0 (source)
  • (concat)
  • (concat x)
  • (concat x y)
  • (concat x y & zs)
Returns a lazy seq representing the concatenation of the elements in the supplied colls.
12 Examples

user=> (concat [1 2] [3 4])
(1 2 3 4)

user=> (into [] (concat [1 2] [3 4]))
[1 2 3 4]

user=> (concat [:a :b] nil [1 [2 3] 4])
(:a :b 1 [2 3] 4)

=> (concat [1] [2] '(3 4) [5 6 7] #{9 10 8})
(1 2 3 4 5 6 7 8 9 10)
;; The last three elements might appear in a different order.
user=> (concat "abc" "def")
(\a \b \c \d \e \f)
user=> (apply concat '(([1 2]) ([3 4] [5 6]) ([7 8])))
([1 2] [3 4] [5 6] [7 8])
user=> (concat '(1 2 3) '(4 5 6))
;; (1 2 3 4 5 6)
user=> (concat [1 2 3] [4 5 6])
;; (1 2 3 4 5 6)
(concat {:a "A" :b "B" :c "C"} {:d "D" :e "E"})
;; ([:a "A"] [:b "B"] [:c "C"] [:d "D"] [:e "E"])
(defn padding-right [s width pad] 
  (apply str (take width (concat s (repeat pad)))))

(padding-right "Clojure" 10 " ")
;; "Clojure   "
;; beware! concat returns a lazy 'sequence'. 

(conj (concat [1 2] [3 4]) 5) ; doesn't return (1 2 3 4 5)
;;=>(5 1 2 3 4)
;; Here is a good article by Stuart Sierra in his "Clojure Dont's" series
;; on concat, and how using it in certain ways can lead to surprisingly large
;; stack usage:
;; https://stuartsierra.com/2015/04/26/clojure-donts-concat

(first (reduce concat (map next-results (range 1 4000))))
;; StackOverflowError   clojure.core/seq (core.clj:133)

(nth (iterate #(concat % [1 2 3]) [1 2 3]) 4000)
;; StackOverflowError   clojure.core/seq (core.clj:133)
;; list 1
(def list1 (list 'let ['x 10]))
;; => #'user/list1
list1
;; => (let [x 10])

;; list 2
(def list2 (list '(println "x:" x) '(println "Bye!")))
;; => #'user/list2
list2
;; => ((println "x:" x) (println "Bye!"))

;; ***
;; concat all the elements of list1 & list2, and return a new list 
;; In this case -  let + [x 10] + (println "x:" x) + (println "Bye!")

(concat list1 list2)
;; => (let [x 10] (println "x:" x) (println "Bye!"))

(eval (concat list1 list2))
;; => x: 10
;;    Bye!
;;    nil


;; see also - quote
(concat [1 2 3] nil)
;; => (1 2 3)

(concat [1 2 3] [nil])
;; => (1 2 3 nil)
(concat [1 2 3] 4 5)
;; => (1Error printing return value (IllegalArgumentException) at clojure.lang.RT/seqFrom (RT.java:577).
;;    Don't know how to create ISeq from: java.lang.Long

(concat [1 2 3] [4 5])
;; => (1 2 3 4 5)
See Also

conj[oin]. Returns a new collection with the xs 'added'. (conj nil item) returns (item). (co...

Added by Olivenmann

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

Expands to code which yields a lazy sequence of the concatenation of the supplied colls. Each col...

Added by reborg

Yields the unevaluated form. See http://clojure.org/special_forms for more information.

Returns the result of applying concat to the result of applying map to f and colls. Thus function...

Added by MicahElliott

Returns a map with the keys mapped to the corresponding vals.

Added by MicahElliott

A transducer which concatenates the contents of each input, which must be a collection, into the r...

Added by mars0i

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

Added by mars0i
0 Notes
No notes for concat