More Collection Functions

user=> (max 0 1 2)
2
user=> (max 0 1 2 "a")

ClassCastException java.base/java.lang.String cannot be cast to java.base/java.lang.Number  clojure.lang.Numbers.gt (Numbers.java:229)
user=> (max [0 1 2])
[0 1 2]
user=> (max [0 1 2] "a")

ClassCastException java.base/java.lang.String cannot be cast to java.base/java.lang.Number  clojure.lang.Numbers.gt (Numbers.java:229)
user=> (apply max [0 1 2])
2
user=> (vector (concat [1 2 3 4] [5]))
[(1 2 3 4 5)]
user=> (vec (concat [1 2 3 4] [5]))
[1 2 3 4 5]
user=> (concat [1 2] [3 4])
(1 2 3 4)
user=> (concat 1 2 3)

IllegalArgumentException Don't know how to create ISeq from: java.lang.Long  clojure.lang.RT.seqFrom (RT.java:542)
user=> (into (hash-map) [ {:a 1} {:c 3} {:b 2} ] )
{:a 1, :c 3, :b 2}
user=> (into (hash-map) [ :a 1 :c 3 :b 2 ] )

IllegalArgumentException Don't know how to create ISeq from: clojure.lang.Keyword  clojure.lang.RT.seqFrom (RT.java:542)
user=> (into (hash-map) [ [:a 1] [:c 3] [:b 2] ] )
{:a 1, :c 3, :b 2}
user=> (into (hash-map) [:a 1] [:c 3] [:b 2]  )

ArityException Wrong number of args (4) passed to: core/into  clojure.lang.AFn.throwArity (AFn.java:429)
user=> (into (hash-map) '( [:a 1] [:c 3] [:b 2] ) )
{:a 1, :c 3, :b 2}
user=> (into {} '( [:a 1] [:c 3] [:b 2] ) )
{:a 1, :c 3, :b 2}
user=> (into [1 2 3] 4)

IllegalArgumentException Don't know how to create ISeq from: java.lang.Long  clojure.lang.RT.seqFrom (RT.java:542)
;; "into" can be used to add to a collection, but not the best way
user=> (into [1 2 3] [4])
[1 2 3 4]
user=> (into [1 2 3] (vector 4))
[1 2 3 4]
user=> (into #{1 2 3} (hash-set 4 5))
#{1 4 3 2 5}


user=> (conj [1 2 3 4] 0)
[1 2 3 4 0]
user=> (conj '(1 2 3) 4)
(4 1 2 3)
user=> (concat [1 2 3 4] [5])
(1 2 3 4 5)
user=> (conj [1 2 3 4] 5)
[1 2 3 4 5]
user=> (vector (concat [1 2 3 4] [5]))
[(1 2 3 4 5)]
user=> (vec (concat [1 2 3 4] [5]))
[1 2 3 4 5]
user=> (vector 1 2 3)
[1 2 3]
user=> (vec 1 2 3)

user=> (cons 0 [1 2 3 4]) ;; puts 0 at front, returns list
ArityException Wrong number of args (3) passed to: core/vec  clojure.lang.AFn.throwArity (AFn.java:429)
(0 1 2 3 4)
user=> (vec (cons 0 [1 2 3 4])) ;; puts 0 at front, converts to vector
[0 1 2 3 4]
user=> (conj  [1 2 3 4] 0) ;; puts 0 at the end
[1 2 3 4 0]
user=> (cons 0 '(1 2 3 4))
(0 1 2 3 4)
user=> (conj '(1 2 3 4) 0)
(0 1 2 3 4)
user=> 
;; cons always returns list
;; conj returns the same type
user=> (cons 0 [1 2 3 4])
(0 1 2 3 4)
user=> (cons 0 '(1 2 3 4))
(0 1 2 3 4)
user=> (conj [1 2 3 4] 0)
[1 2 3 4 0]
user=> (conj '(1 2 3 4) 0)
(0 1 2 3 4)
user=> (cons 0 #{1 2 3})
(0 1 3 2)
user=> (conj #{1 2 3} 4 5)
#{1 4 3 2 5}
;; and conj can take multiple elements to add
user=> (conj [1 2 3 4] 0 5 6)
[1 2 3 4 0 5 6]
;; so cons for list, conj for vectors
;; cons, arg is in front and added to front
;; conj, arg is in back, and added to back

I am going through Clojure For The Brave and True, and I have included some of the functions that I encounter while reading the book.

 

You’re welcome.