Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Clojure's distinct? function on a collection?

Tags:

clojure

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?

like image 996
Robert Campbell Avatar asked Nov 08 '09 22:11

Robert Campbell


1 Answers

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
like image 53
Mike Douglas Avatar answered Oct 21 '22 21:10

Mike Douglas