将元素放在集合的尾部 [英] Put an element to the tail of a collection
问题描述
我发现自己做了很多:
(concat coll [e])
收集和单一元素。
在Clojure中有这样的功能吗?我知道conj工作最好的向量,但我不知道前面coll将被使用。例如,它可以是一个向量,列表或排序集。
Is there a function for doing this in Clojure? I know conj does the job best for vectors but I don't know up front which coll will be used. It could be a vector, list or sorted-set for example.
推荐答案
列表,seq),而其他人可以便宜地添加到后面(向量,队列,kinda-sorta lazy-seqs)。而不是使用concat,如果可能,你应该安排使用这些类型之一(向量是最常见的),并只是conj它:(conj [1 2 3] 4)
产生 [1 2 3 4]
,而(conj(1 2 3)4)
c $ c>(4 1 2 3)。
Some types of collections can add cheaply to the front (lists, seq), while others can add cheaply to the back (vectors, queues, kinda-sorta lazy-seqs). Rather than using concat, if possible you should arrange to be working with one of those types (vector is most common) and just conj to it: (conj [1 2 3] 4)
yields [1 2 3 4]
, while (conj '(1 2 3) 4)
yields (4 1 2 3)
.
这篇关于将元素放在集合的尾部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!