简体   繁体   English

我如何使用Clojure的独特之处? 集合上的功能?

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

The Clojure distinct? Clojure 与众不同? method doesn't take a collection, but rather a list of args 方法不采用集合,而是采用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. 因此(distinct?0 0 0 0)正确返回false ,而(distinct?[0 0 0 0])返回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? 在一个集合上,以便传递一个集合[0 0 0 0]将返回false,因为该集合包含重复项?

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. 我确实意识到该函数正在正常运行,但我正在寻找一种技巧来将它应用于集合的内容而不是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 . 如果要将参数作为seq传递给函数,请使用apply

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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