Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put an element to the tail of a collection

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 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.

like image 853
Michiel Borkent Avatar asked Apr 20 '11 17:04

Michiel Borkent


1 Answers

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).

like image 102
amalloy Avatar answered Sep 23 '22 09:09

amalloy