Clojure Tutorial on Clojure Sets

sets in clojure are a set of unique values. sets are created in clojure with the help of the set command.

example

following is an example of the creation of sets in clojure.

(ns clojure.examples.example
   (:gen-class))
(defn example []
   (println (set '(1 1 2 2))))
(example)

output

the above code produces the following output.

#{1,2}

following are the methods available in clojure for sets.

sr.no. sets & description
1 sorted-set

returns a sorted set of elements.

2 get

returns the element at the index position.

3 contains?

finds out whether the set contains a certain element or not.

4 conj

appends an element to the set and returns the new set of elements.

5 disj

disjoins an element from the set.

6 union

return a set that is the union of the input sets

7 difference

return a set that is the first set without elements of the remaining sets.

8 intersection

return a set that is the intersection of the input sets.

9 subset?

is set1 a subset of set2?

10 superset?

is set1 a superset of set2?