似乎我必须缺少一些明显常用的习语,但缺少这两个功能似乎有点令我感到困惑.有some
但它返回nil
而不是False
,为什么不是一个any?
功能?
Art*_*ldt 12
some
被认为与存在时相同any?
.有一个密切命名的功能not-any?
,只是some
在引擎盖下调用:
(source not-any?)
(def
...
not-any? (comp not some))
Run Code Online (Sandbox Code Playgroud)
你可以简单地写任何:
(def any? (comp boolean some))
Run Code Online (Sandbox Code Playgroud)
补丁欢迎:)只需填写并首先在贡献者协议中邮寄.
考虑到not-any?
从1.0开始包含该函数,关于命名的观点尤其正确
(defn any? [pred col] (not (not-any? pred col)))
(any? even? [1 2 3])
true
(any? even? [1 3])
false
Run Code Online (Sandbox Code Playgroud)
我想没有人来补丁发送?(提示暗示轻推)
当使用基于的任何代码some
(not-any?
在引擎盖下调用一些)时要小心匹配pred和col的类型或使用捕获类型异常的pred
(if (some odd? [2 2 nil 3]) 1 2)
No message.
[Thrown class java.lang.NullPointerException]
Run Code Online (Sandbox Code Playgroud)
ps:这个例子来自clojure 1.2.1
小智 9
nil
评估为假. (if nil 1 2)
评估为2.
some
返回满足谓词的第一个元素.任何没有nil
或false
评估为真的东西.所以(if (some odd? [2 2 3]) 1 2)
评估为1.
您正在寻找some-fn
,尽管您必须通过它生成的谓词的返回值进行线程化boolean
。
user> (doc every-pred)
-------------------------
clojure.core/every-pred
([p] [p1 p2] [p1 p2 p3] [p1 p2 p3 & ps])
Takes a set of predicates and returns a function f that returns true if all of its
composing predicates return a logical true value against all of its arguments, else it returns
false. Note that f is short-circuiting in that it will stop execution on the first
argument that triggers a logical false result against the original predicates.
nil
user> (doc some-fn)
-------------------------
clojure.core/some-fn
([p] [p1 p2] [p1 p2 p3] [p1 p2 p3 & ps])
Takes a set of predicates and returns a function f that returns the first logical true value
returned by one of its composing predicates against any of its arguments, else it returns
logical false. Note that f is short-circuiting in that it will stop execution on the first
argument that triggers a logical true result against the original predicates.
nil
Run Code Online (Sandbox Code Playgroud)
这些函数的相似性与every?
和some
相似。