Clojure 中是否有类似于 Python 的 any
和 all
函数的内置函数?
例如,在 Python 中,它是 all([True, 1, 'non-empty string']) == True
。
最佳答案
(每?f 个数据)
[ docs ] 与 all(f(x) for x in data)
相同。
(一些 f 数据)
[ docs ] 与 any(f(x) for x in data)
类似,只是它返回 f(x)
的值(必须为真),而不仅仅是 true
。
如果您想要与 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
关于python - Clojure 相当于 Python 的 "any"和 "all"函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7871295/