为什么矢量的concat评估为列表?

Sta*_*ked 28 clojure

在矢量上调用concat会返回一个列表.作为一个总的菜鸟我会期望结果也是一个矢量.为什么要转换成列表?

例:

user=> (concat [1 2] [3 4] [5 6])
(1 2 3 4 5 6)
; Why not: [1 2 3 4 5 6] ?
Run Code Online (Sandbox Code Playgroud)

dno*_*len 36

concat返回一个懒惰的序列.

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.
Run Code Online (Sandbox Code Playgroud)

您可以将其转换回带有以下内容的向量:

user=> (into [] (concat [1 2] [3 4] [5 6]))
[1 2 3 4 5 6]
Run Code Online (Sandbox Code Playgroud)

使用瞬态,所以它很快.

  • 对于性能非常相似的稍短代码也有`vec`. (18认同)