Some Work With Collections

Here are some functions used for working with collections (vectors, maps, sets). I did not include too much with lists, since I think a lot of Clojure people prefer vectors.

 

user=> (def letter-vec (vector "a", "b", "c", "d"))
#'user/letter-vec
;; use nth function to get elements, 0-based
user=> (nth letter-vec 0)
"a"
user=> (nth letter-vec 1)
"b"
user=> (nth letter-vec 2)
"c"
user=> (nth letter-vec 3)
"d"
user=> (nth letter-vec 4)
IndexOutOfBoundsException   clojure.lang.PersistentVector.arrayFor (PersistentVector.java:158)
user=> 
;; cycle through vector 
user=> (for [x letter-vec] (println "Next element: ", x))
Next element:  a
Next element:  b
Next element:  c
Next element:  d
(nil nil nil nil)

;; doseq kind of does the same thing
user=> (doseq [x letter-vec] (println "Next element: ", x))
Next element:  a
Next element:  b
Next element:  c
Next element:  d
nil

user=> (def my-map {:fname "George" :lname "Washington" :residence "Mt Vernon" :teeth "Not wood"})
#'user/my-map
user=> (keys my-map)
(:fname :lname :residence :teeth)
user=> (vals my-map)
("George" "Washington" "Mt Vernon" "Not wood")
user=> (doseq [k (keys my-map) ] 
  (println "Key: ", k, ", Value: " , (get my-map k)))
Key:  :fname , Value:  George
Key:  :lname , Value:  Washington
Key:  :residence , Value:  Mt Vernon
Key:  :teeth , Value:  Not wood
nil

user=> (for [k (keys my-map) ] 
  (println "Key: ", k, ", Value: " , (get my-map k)))
Key:  :fname , Value:  George
Key:  :lname , Value:  Washington
Key:  :residence , Value:  Mt Vernon
Key:  :teeth , Value:  Not wood
(nil nil nil nil)


user=> (def s-map (sorted-map :a "AA" :b "BB" :c 44))
#'user/s-map
user=> s-map
{:a "AA", :b "BB", :c 44}
;; sorted-map looks at keys, not values
user=> (def s-map2 (sorted-map :a "AA" :b "BB" "c" 44))

ClassCastException clojure.lang.Keyword cannot be cast to java.base/java.lang.String  java.lang.String.compareTo (String.java:123)


;; sorted set items must all be of same type
user=> (def a-set (sorted-set "kurt vonnegut" 20 :icicle))
ClassCastException java.base/java.lang.String cannot be cast to java.base/java.lang.Number  clojure.lang.Util.compare (Util.java:152)

user=> (def a-set (hash-set "kurt vonnegut" 20 :icicle))
#'user/a-set
user=> (class a-set)
clojure.lang.PersistentHashSet
user=> (def b-set #{"kurt vonnegut" 20 :icicle})
#'user/b-set
;; hash sets are the default
user=> (class b-set)
clojure.lang.PersistentHashSet

;; doseq works on sets too
user=> (doseq [x b-set] (println "Next element: ", x))
Next element:  20
Next element:  :icicle
Next element:  kurt vonnegut

;; so does "for"
user=> (for [x b-set] (println "Next element: ", x))
Next element:  20
Next element:  :icicle
Next element:  kurt vonnegut
(nil nil nil)

user=> (def num-set #{1 3 5 7 9})
#'user/num-set
user=> num-set
#{7 1 3 9 5}
user=> (conj num-set 4)
#{7 1 4 3 9 5}
user=> (conj num-set 4)
#{7 1 4 3 9 5}
user=> (conj num-set 8)
#{7 1 3 9 5 8}
user=> (def sorted-num-set (sorted-set 9 3 7 1 5))
#'user/sorted-num-set
user=> sorted-num-set
#{1 3 5 7 9}
user=> (conj sort
sort             sort-by          sorted-map       sorted-map-by    sorted-num-set   sorted-set       sorted-set-by    sorted?          
user=> (conj sorted
sorted-map       sorted-map-by    sorted-num-set   sorted-set       sorted-set-by    sorted?          
user=> (conj sorted-num-set 4)
#{1 3 4 5 7 9}
user=> (contains? sorted-num-set 3)
true
user=> (contains? sorted-num-set 'a')

ClassCastException java.base/java.lang.Long cannot be cast to clojure.lang.Symbol  clojure.lang.Symbol.compareTo (Symbol.java:105)
;; Wouldn't "false" be a better response? Perhaps it's macro time!!
user=> (contains? sorted-num-set :a)

ClassCastException java.base/java.lang.Long cannot be cast to clojure.lang.Keyword  clojure.lang.Keyword.compareTo (Keyword.java:114)
user=> (contains? a-set :a)
false
user=> (keyword? :a)
true
user=> (def keyword-set (sorted-set :a :d :c :b ))
#'user/keyword-set
user=> (contains? keyword-set :s)
false
user=> (contains? keyword-set :d)
true
user=> (contains? keyword-set 3)

ClassCastException clojure.lang.Keyword cannot be cast to java.base/java.lang.Number  clojure.lang.Util.compare (Util.java:152)
user=> keyword-set
#{:a :b :c :d}
user=> (:a keyword-set)
:a
user=> (:k keyword-set)
nil
user=> (get #{:a :b} "kurt vonnegut")
nil

You’re welcome.

There are 576 words in this article.