philipbohun.com

Glojure by Example: Sets

A set holds each value once. contains? tests membership. conj and disj return a new set. clojure.set has the usual set algebra. Printed sets have no stable order, so this program sorts the results it displays.

(require '[clojure.set :as sets])

(def colors #{:red :green :blue})
(println (contains? colors :green))
(println (count (disj colors :green)))
(println (sort (sets/union #{1 2} #{2 3})))
(println (sort (sets/difference #{1 2 3} #{2})))
(println (sort (sets/intersection #{1 2} #{2 3})))

Union, difference, and intersection.

$ glj sets.glj
true
2
(1 2 3)
(1 3)
(2)

Next example: Sequences.