Clojure: cons (seq) vs. conj (list)

后端 未结 5 672
忘掉有多难
忘掉有多难 2020-12-04 05:17

I know that cons returns a seq and conj returns a collection. I also know that conj \"adds\" the item to the optimal end of the colle

5条回答
  •  庸人自扰
    2020-12-04 06:00

    Another difference is that because conj takes a sequence as the first argument, it plays nicely with alter when updating a ref to some sequence:

    (dosync (alter a-sequence-ref conj an-item))
    

    This basically does (conj a-sequence-ref an-item) in a thread-safe manner. This wouldn't work with cons. See the chapter on Concurrency in Programming Clojure by Stu Halloway for more info.

提交回复
热议问题