conj adds an element to a data structure in a place most efficient for the
particular type of data structure.  For lists, this is at the beginning.
For vectors, it is at the end:

user=> (conj (conj (conj '(1 2 3) 4) 6) 7)
(7 6 4 1 2 3)
user=> (conj (conj (conj [1 2 3] 4) 6) 7)
[1 2 3 4 6 7]

If you want something that adds to the end of a list, or the beginning of a
vector, you must implement it yourself, and it won't be O(1) as the above
are (OK, the vector version above is not quite O(1), but it is O(log n)).
Here are functions that can do the job, but take time linear in the size of
the list/vector being added to:

user=> (defn add-at-end [l item] (concat l (list item)))
#'user/add-at-end
user=> (add-at-end '(1 2 3) 4)
(1 2 3 4)
user=> (defn add-at-front [v item] (vec (cons item (seq v))))
#'user/add-at-front
user=> (add-at-front [1 2 3] 4)
[4 1 2 3]

Andy



On Sat, Dec 10, 2011 at 3:13 AM, Michael Jaaka <michael.ja...@googlemail.com
> wrote:

> Is there something like:
>
> (defn snoc[ col item ]
>        (lazy-seq
>                (if (seq col)
>                        (let[ [f &  r] col ]
>                                (if (seq r)
>                                        (cons f (snoc r item))
>                                        (cons f [item])))
>                        [item])))
>
> already here?
>
>
> (snoc (snoc (snoc [ 1 2 3] 4) 6) 7)
>
> gives:
>
> (1 2 3 4 5 6 7)
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to