If we want to get the values from a set that are not part of one or more other sets we must use the difference
function in the clojure.set
namespace. The function returns a set with all values from the first set that are different from values in other sets.
In the following example we use the difference
with several sets:
(ns mrhaki.set.difference (:require [clojure.set :as set] [clojure.test :refer [is]])) ;; The difference function will take a first set ;; and leave out elements that are in the following set(s). (is (= #{"Java"} (set/difference #{"Java" "Clojure" "Groovy"} #{"Kotlin" "Groovy" "Clojure"}))) (is (= #{"Java"} (set/difference #{"Java" "Clojure" "Groovy"} #{"Kotlin" "Groovy"} #{"Clojure"}))) ;; When other sets do not contain values ;; from the first set, the result is the original set. (is (= #{1 2 3} (set/difference #{1 2 3} #{4 5})))
Written with Clojure 1.10.1.