Put an element to the tail of a collection

后端 未结 4 854
北海茫月
北海茫月 2021-02-02 05:25

I find myself doing a lot of:

(concat coll [e]) where coll is a collection and e a single element.

Is there a function for doing this in Clojure? I

4条回答
  •  醉梦人生
    2021-02-02 06:04

    Some types of collections can add cheaply to the front (lists, seqs), 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).

提交回复
热议问题