Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does concat on vectors evaluate to a list?

Tags:

clojure

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] ?
like image 926
StackedCrooked Avatar asked Apr 26 '10 00:04

StackedCrooked


1 Answers

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.

like image 135
dnolen Avatar answered Nov 16 '22 17:11

dnolen