DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Difference of cons and conj in Clojure


conj put collection as its first argument while cons put collection as its second argument.

Synopsis:

conj element1 element2 ...

cons element

Meanwhile this post from http://stackoverflow.com/questions/3008411/clojure-seq-cons-vs-list-conj give other 2 important difference:

One difference is that conj accepts any number of arguments to insert into a collection, while cons takes just one:

(conj '(1 2 3) 4 5 6) ; => (6 5 4 1 2 3) (cons 4 5 6 '(1 2 3)) ; => IllegalArgumentException due to wrong arity Another difference is in the class of the return value: (class (conj '(1 2 3) 4)) ; => clojure.lang.PersistentList (class (cons 4 '(1 2 3)) ; => clojure.lang.Cons



Published

Sep 26, 2013

Last Updated

Sep 26, 2013

Category

Tech

Tags

  • clojure 26

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor