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?

Advertisements