【问题标题】:into list vs. into vector in Clojure在 Clojure 中进入列表与进入向量
【发布时间】:2015-10-04 17:06:08
【问题描述】:

你能解释一下 Clojure 中的这种行为吗?

user=> (into [1 2 3] ["a" "b"])
[1 2 3 "a" "b"]

但是

user=> (into '(1 2 3) ["a" "b"])
("b" "a" 1 2 3)

into with vector 会附加项目是可以理解的,但是为什么使用into with list 首先会恢复项目顺序,然后将其添加到列表中?

【问题讨论】:

    标签: data-structures clojure functional-programming


    【解决方案1】:

    into 使用conj 将项目添加到源集合中。 conj 将项目附加到列表的前面和向量的末尾。 Clojure 列表是不可变的单链表,因此添加到列表末尾将是一个 O(n) 操作。在前面插入是一个常数时间的操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多