yegor256
yegor256

Reputation: 105053

how to disjoin two sets?

I have two sets and need to create a third one, which will include elements from the first one that are absent in the second one:

(? #{"a" "b" "c"} #{"b"}) ; -> ["a" "c"]

I know about disj, but it works only when the second argument is an element, not a set.

Upvotes: 1

Views: 196

Answers (2)

NielsK
NielsK

Reputation: 6956

If there wasn't a difference function, you might have created it easily with reduce:

=> (reduce disj #{"a" "b" "c" "d"} #{"b" "d"})
#{"a" "c"}

Reduce takes a function, an initial 'accumulator' and a collection it maps over to modify the accumulator. In this case it would use the first set as an accumulator, remove the first item from the second set from it, then the next, etc.

Upvotes: 2

soulcheck
soulcheck

Reputation: 36767

If you have two sets, you can use set difference:

user=> (require 'clojure.set)
user=> (difference #{"a" "b" "c"} #{"b"})
#{"a" "c"}

Upvotes: 7

Related Questions