Clojure – Sets

Clojure lists sets

In this guide, we will discuss 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
1sorted-set
Returns a sorted set of elements.
2get
Returns the element at the index position.
3contains?
Finds out whether the set contains a certain element or not.
4conj
Appends an element to the set and returns the new set of elements.
5disj
Disjoins an element from the set.
6union
Return a set that is the union of the input sets
7difference
Return a set that is the first set without elements of the remaining sets.
8intersection
Return a set that is the intersection of the input sets.
9subset?
Is set1 a subset of set2?
10superset?
Is set1 a superset of set2?

Next Topic : Click Here

This Post Has One Comment

Leave a Reply