gpt4 book ai didi

clojure - 如何使用 Clojure 的 distinct?集合上的函数?

转载 作者:行者123 更新时间:2023-12-04 05:27:37 25 4
gpt4 key购买 nike

Clojure 清楚的? 方法不接受一个集合,而是一个参数列表

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

所以 (distinct? 0 0 0 0) 正确返回 , while (distinct? [0 0 0 0]) 返回真。我如何使用 清楚的? 在集合上传递一个集合 [0 0 0 0] 将返回 false,因为该集合包含重复项?

我确实意识到该函数正在正确执行,但我正在寻找将其应用于集合内容而不是参数列表的技巧。

作为一种解决方法,我目前有
(defn coll-distinct? [coll]
(= (distinct coll) coll))

但我觉得我错过了一种更优雅的重用方式 清楚的?

最佳答案

如果要将参数作为 seq 传递给函数,请使用 apply .

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

关于clojure - 如何使用 Clojure 的 distinct?集合上的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1698122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com