与Clojure的序列不一致? [英] Inconsistency with Clojure's sequences?

查看:118
本文介绍了与Clojure的序列不一致?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Clojure:

1:13 user=> (first (conj '(1 2 3) 4))
4
1:14 user=> (first (conj [1 2 3] 4))
1
; . . .
1:17 user=> (first (conj (seq [1 2 3]) 4))
4



I understand what is going on, but should this work differently?

推荐答案

conj (来自 clojure.org ):


conj [oin]。返回一个新的集合xs
'added'。 (conjnil item)返回(item)。

conj[oin]. Returns a new collection with the xs 'added'. (conj nil item) returns (item). The 'addition' may happen at different 'places' depending on the concrete type.

添加可能会在不同的地方 元素到一个向量的结尾,而在列表的开头更有效。 conj 使用任何对您提供的数据结构最有效的方法。

It's more efficient to "add" elements to the end of a vector, while it's more efficient to do so at the beginning of lists. conj uses whatever is the most efficient for the data structure you give it.

code>'(1 2 3)(seq [1 2 3]) / code>(请参阅 seq的文档? ), [1 2 3] 不。

In the examples you give, '(1 2 3) and (seq [1 2 3]) both implement ISeq (see documentation for seq?), while [1 2 3] doesn't.

Clojure的 conj 最终调用 cons 方法(不要与 / code> function - 这个方法是内部clojure代码)对底层数据结构的影响;对于向量( PersistentVector ), cons 向末尾添加元素,而对于列表, cons 方法返回一个新元素作为头元素的新列表,尾)。

Clojure's conj ultimately calls the cons method (not to be confused with the cons function - this method is internal clojure code) on the underlying data structure; for vectors (PersistentVector), cons adds elements to the end, while for lists they're added to the front (the cons method for PersistentLists returns a new list with the new element as its head, and the existing list as its tail).

这篇关于与Clojure的序列不一致?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆