1.3.0 permalink Arrow_down_16x16
  • (conj coll x)
  • (conj coll x & xs)
conj[oin]. Returns a new collection with the xs
'added'. (conj nil item) returns (item). The 'addition' may
happen at different 'places' depending on the concrete type.

4 Examples top

  • user=> (conj [1 2 3] 4)
    [1 2 3 4]
    
    user=> (conj '(1 2 3) 4)
    (4 1 2 3)
    
    user=> (conj ["a" "b" "c"] "d")
    ["a" "b" "c" "d"]
    
    user=> (conj [1 2] 3 4)               
    [1 2 3 4]
    
    user=> (conj [[1 2] [3 4]] [5 6])       
    [[1 2] [3 4] [5 6]]
    
    ;; Maps only take vectors of length exactly 2
    user=> (conj {1 2, 3 4} [5 6])
    {5 6, 1 2, 3 4}
    
    user=> (conj {:firstname "John" :lastname "Doe"} {:age 25 :nationality "Chinese"})
    {:nationality "Chinese", :age 25, :firstname "John", :lastname "Doe"}
    
    ;; conj on a set
    user=> (conj #{1 3 4} 2)
    #{1 2 3 4}
    
    
  • ;conjoin shows similar behaviour to cons
    (conj ["a" "b" "c"] ["a" "b" "c"] )
    user=> ["a" "b" "c" ["a" "b" "c"]]
  • ; conj nil with x or xs
    user=> (conj nil 3)
    (3)
    user=> (conj nil 3 4)
    (4 3)
  • ; maps and sets are treated differently
    user=> (conj {1 2} {3 4})
    {3 4, 1 2}   ; the contents of {3 4} are added to {1 2}
    
    user=> (conj #{1 2} #{3})
    #{1 2 #{3}}  ; the whole set #{3} is added to #{1 2}
    
    user=> (clojure.set/union #{1 2} #{3})
    #{1 2 3}  ; must use (clojure.set/union) to merge sets, not conj
    
Log in to add / edit an example.

See Also top

  • 1
    clojure.core/cons

    Returns a new seq where x is the first element and seq is the res

  • 0
    clojure.core/peek

    For a list or queue, same as first, for a vector, same as, but much

  • 0
    clojure.core/into

    Returns a new coll consisting of to-coll with all of the items of f

  • 0
    clojure.core/pop

    For a list or queue, returns a new list/queue without the first ite

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:75 top

(def
 ^{:arglists '([coll x] [coll x & xs])
   :doc "conj[oin]. Returns a new collection with the xs
    'added'. (conj nil item) returns (item).  The 'addition' may
    happen at different 'places' depending on the concrete type."
   :added "1.0"
   :static true}
 conj (fn ^:static conj 
        ([coll x] (. clojure.lang.RT (conj coll x)))
        ([coll x & xs]
         (if xs
           (recur (conj coll x) (first xs) (next xs))
           (conj coll x)))))
Vars in clojure.core/conj:
Used in 0 other vars

Comments top

No comments for conj. Log in to add a comment.