32

Calling concat on vectors returns a list. Being a total noob I would expect that the result would also be a vector. Why the conversion to list?

Example:

user=> (concat [1 2] [3 4] [5 6])
(1 2 3 4 5 6)
; Why not: [1 2 3 4 5 6] ?
StackedCrooked
  • 34,653
  • 44
  • 154
  • 278

1 Answers1

41

concat returns a lazy sequence.

user=> (doc concat)
-------------------------
clojure.core/concat
([] [x] [x y] [x y & zs])
  Returns a lazy seq representing the concatenation of the elements in the supplied colls.

you can convert it back to a vector with into:

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

into uses transients so it's pretty quick about it.

dnolen
  • 18,496
  • 4
  • 62
  • 71
  • 20
    There's also `vec` for slightly shorter code with very similar performance. – Michał Marczyk Apr 26 '10 at 01:20
  • I believe the 'why' is because it returns a LazySeq, which is not really a list. Besides, Clojure vectors are not lazy. See http://stackoverflow.com/q/12206806/1814970. – marcelocra Apr 02 '15 at 04:04
  • The `concat` returnes only a sequence as mentioned in its `doc`. as Michal mentioned already, you can send the result of your `concat` to the `vec` function the get a vector back: `(vec (concat [1 2] [3 4] [5 6]))` (Thank you @Mars for your suggestion) –  Aug 22 '15 at 23:34