Clojure 相当于 Python 的“any”和“全部”功能?

发布于 2024-12-11 07:02:21 字数 158 浏览 0 评论 0原文

Clojure 中是否有类似于 Python 的 anyall 函数的内置函数?

例如,在 Python 中,它是 all([True, 1, 'non-empty string']) == True

Are there built in functions in Clojure similar to Python's any and all functions?

For example, in Python, it's all([True, 1, 'non-empty string']) == True.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

許願樹丅啲祈禱 2024-12-18 07:02:21

(每个?f数据) [文档]< /sup> 与 all(f(x) for x in data) 相同。

(一些f数据) [docs] 与 any(f(x) for x in data) 类似,只是它返回 f(x) 的值(必须为真值),而不仅仅是

如果您想要与 Python 中完全相同的行为,可以使用 identity 函数,该函数将仅返回其参数(相当于 (fn [x] x))。

user=> (every? identity [1, true, "non-empty string"])
true
user=> (some identity [1, true "non-empty string"])
1
user=> (some true? [1, true "non-empty string"])
true

(every? f data) [docs] is the same as all(f(x) for x in data).

(some f data) [docs] is like any(f(x) for x in data) except that it returns the value of f(x) (which must be truthy), instead of just true.

If you want the exact same behaviour as in Python, you can use the identity function, which will just return its argument (equivalent to (fn [x] x)).

user=> (every? identity [1, true, "non-empty string"])
true
user=> (some identity [1, true "non-empty string"])
1
user=> (some true? [1, true "non-empty string"])
true
温柔戏命师 2024-12-18 07:02:21

在 clojure 中,andor 与 python 的 allany 非常相似,但需要注意的是(就像 < code>clojure.core/some)它们返回满足它的元素...因此您可以将它与 boolean 一起使用来转换它

(boolean (or "" nil false)) ; "" is truthy in clojure
; => true
(boolean (and [] "" {} () 0)) ; also [], {}, () and 0 are truthy
; => true

我使用 boolean > 而不是 true? 因为后者将返回 true iff 参数的值是 true...所以 boolean 更类似于 python 的 bool 因为它评估真实性

some & 不同每个? & or 是宏,因此如果您总是想将结果转换为布尔值,则不能简单地执行 (def any (comp boolean or)) ,而是必须定义一个宏就像

(defmacro any [& v] `(boolean (or ~@v)))
(defmacro all [& v] `(boolean (and ~@v)))

宏的副作用/优点一样,是它们是惰性的/可以短路(就像 python 的 andor 一样,它们是中缀二元运算符)

(any "" (/ 1 0))
; => true
(all nil (/ 1 0))
; => false

他们就像python 的 anyall,即使在 python 中不带参数调用时

(any)
; => false
(all)
; => true

>>> any([])
False    
>>> all([])
True

如果您更喜欢调用 any/all 使用单个列表/序列参数,您可以简单地执行以下操作:

(defmacro all [v] `(boolean (and ~@v)))

(all [])
; => true
(all [nil (/ 1 0)])    
; => false

In clojure and and or are quite similar to python's all and any, with the caveat that (just like clojure.core/some) they return the element that will satifsy it... thus you can use it together with boolean to convert it

(boolean (or "" nil false)) ; "" is truthy in clojure
; => true
(boolean (and [] "" {} () 0)) ; also [], {}, () and 0 are truthy
; => true

I use boolean instead of true? since the latter will return true iff the argument is the value true... so boolean is more akin to python's bool in that it evaluates the truthyness

Unlike some & every?, and & or are macros, so if you always want to convert the result to a boolean, you cannot simply do (def any (comp boolean or)) but you have to define a macro like

(defmacro any [& v] `(boolean (or ~@v)))
(defmacro all [& v] `(boolean (and ~@v)))

a side-effect/advantage of being macros, is that they are lazy/can shortcircuit (just like python's and & or, that are infix binary operators however)

(any "" (/ 1 0))
; => true
(all nil (/ 1 0))
; => false

and they're just like python's any and all, even when called without arguments

(any)
; => false
(all)
; => true

in python:

>>> any([])
False    
>>> all([])
True

If you prefer to have to call any/all with a single list/sequence argument, you can simply do:

(defmacro all [v] `(boolean (and ~@v)))

(all [])
; => true
(all [nil (/ 1 0)])    
; => false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文