tags:

views:

99

answers:

1

The Clojure distinct? method doesn't take a collection, but rather a list of args

(distinct? x)
(distinct? x y)
(distinct? x y & more)

So (distinct? 0 0 0 0) correctly returns false, while (distinct? [0 0 0 0]) returns true. How can I use distinct? on a collection so that passing it a collection [0 0 0 0] would return false since the collection contains duplicates?

I do realize that the function is performing correctly, but I'm looking for a trick to apply it to the contents of a collection instead of a list of args.

As a workaround, I currently have

(defn coll-distinct? [coll]
   (= (distinct coll) coll))

but I feel like I'm missing a more elegant way reusing distinct?

+7  A: 

If you want to pass arguments as a seq to a function, use apply.

(apply distinct? [1 2 3 1])
; false
(apply distinct? [1 2 3])
; true
Mike Douglas