Clojure: cons (seq) vs. conj (list) [英] Clojure: cons (seq) vs. conj (list)
问题描述
我知道 cons
返回一个 seq 而 conj
返回一个集合.我也知道 conj
将项目添加"到集合的最佳末尾,而 cons
总是添加"项目到前面.这个例子说明了这两点:
I know that cons
returns a seq and conj
returns a collection. I also know that conj
"adds" the item to the optimal end of the collection, and cons
always "adds" the item to the front. This example illustrates both of these points:
user=> (conj [1 2 3] 4) ; returns a collection
[1 2 3 4]
user=> (cons 4 [1 2 3]) ; returns a seq
(4 1 2 3)
对于向量、地图和集合,这些差异对我来说很有意义.但是,对于列表,它们似乎完全相同.
For vectors, maps, and sets these differences make sense to me. However, for lists they seem identical.
user=> (conj (list 3 2 1) 4) ; returns a list
(4 3 2 1)
user=> (cons 4 (list 3 2 1)) ; returns a seq
(4 3 2 1)
是否有使用列表的示例,其中 conj
与 cons
表现出不同的行为,或者它们真的可以互换?换种说法,有没有列表和序列不能等价使用的例子?
Are there any examples using lists where conj
vs. cons
exhibit different behaviors, or are they truly interchangeable? Phrased differently, is there an example where a list and a seq cannot be used equivalently?
推荐答案
一个区别是 conj
接受任意数量的参数以插入到集合中,而 cons
接受只有一个:
One difference is that conj
accepts any number of arguments to insert into a collection, while cons
takes just one:
(conj '(1 2 3) 4 5 6)
; => (6 5 4 1 2 3)
(cons 4 5 6 '(1 2 3))
; => IllegalArgumentException due to wrong arity
另一个区别在于返回值的类:
Another difference is in the class of the return value:
(class (conj '(1 2 3) 4))
; => clojure.lang.PersistentList
(class (cons 4 '(1 2 3))
; => clojure.lang.Cons
请注意,这些实际上不能互换;特别是clojure.lang.Cons
没有实现clojure.lang.Counted
,所以对它的count
不再是一个常数时间操作(在这种情况下,它可能会减少到 1 + 3——1 来自第一个元素的线性遍历,3 来自 (next (cons 4 '(1 2 3))
是一个PersistentList
因此Counted
).
Note that these are not really interchangeable; in particular, clojure.lang.Cons
does not implement clojure.lang.Counted
, so a count
on it is no longer a constant time operation (in this case it would probably reduce to 1 + 3 -- the 1 comes from linear traversal over the first element, the 3 comes from (next (cons 4 '(1 2 3))
being a PersistentList
and thus Counted
).
名称背后的意图,我相信,cons
的意思是 cons(truct a seq)1,而 conj
的意思是conj(将项目添加到集合上).由 cons
构造的 seq
以作为其第一个参数传递的元素开始,并具有作为它的 next
/rest
将 seq
应用到第二个参数所产生的结果分开;如上所示,整个内容属于 clojure.lang.Cons
类.相比之下,conj
总是返回一个与传递给它的集合类型大致相同的集合.(粗略地说,因为 PersistentArrayMap
一旦超过 9 个条目就会变成 PersistentHashMap
.)
The intention behind the names is, I believe, that cons
means to cons(truct a seq)1, whereas conj
means to conj(oin an item onto a collection). The seq
being constructed by cons
starts with the element passed as its first argument and has as its next
/ rest
part the thing resulting from the application of seq
to the second argument; as displayed above, the whole thing is of class clojure.lang.Cons
. In contrast, conj
always returns a collection of roughly the same type as the collection passed to it. (Roughly, because a PersistentArrayMap
will be turned into a PersistentHashMap
as soon as it grows beyond 9 entries.)
1 传统上,在 Lisp 世界中,cons
cons(tructs a pair),所以 Clojure 在拥有它的 cons
函数构造一个没有传统 cdr
的 seq.cons
的一般用法表示构建某种类型的记录以将多个值保存在一起"目前在编程语言及其实现的研究中无处不在.这就是提到避免 consing"时的意思.
1 Traditionally, in the Lisp world, cons
cons(tructs a pair), so Clojure departs from the Lisp tradition in having its cons
function construct a seq which doesn't have a traditional cdr
. The generalised usage of cons
to mean "construct a record of some type or other to hold a number of values together" is currently ubiquitous in the study of programming languages and their implementation; that's what's meant when "avoiding consing" is mentioned.
这篇关于Clojure: cons (seq) vs. conj (list)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!