gpt4 book ai didi

clojure - 为什么向量上的 concat 计算结果为列表?

转载 作者:行者123 更新时间:2023-12-03 01:17:25 24 4
gpt4 key购买 nike

对向量调用 concat 将返回一个列表。作为一个十足的菜鸟,我希望结果也是一个向量。为什么要转换为列表?

示例:

user=> (concat [1 2] [3 4] [5 6])
(1 2 3 4 5 6)
; Why not: [1 2 3 4 5 6] ?

最佳答案

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.

您可以使用 into 将其转换回向量:

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

into 使用 transient ,因此速度非常快。

关于clojure - 为什么向量上的 concat 计算结果为列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2710512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com