简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM