假设我map使用以下操作运行以下操作not:
core=> (map (comp not) [true false true false])
(false true false true)
Run Code Online (Sandbox Code Playgroud)
假设我map使用以下操作运行以下操作complement:
core=> (map (complement identity) [true false true false])
(false true false true)
Run Code Online (Sandbox Code Playgroud)
我的问题是:是complement和not有效Clojure中的一样吗?
(除了创建一个compliment有点像compose的行为partial)
lob*_*dik 13
它们是相同的,您可以在几乎没有变化的情况下获得相同的结果(就像您在代码示例中所做的那样).如果我们看一下complement来源,这就变得很清楚:
(source complement)
=>
(defn complement
"Takes a fn f and returns a fn that takes the same arguments as f,
has the same effects, if any, and returns the opposite truth value."
{:added "1.0"
:static true}
[f]
(fn
([] (not (f)))
([x] (not (f x)))
([x y] (not (f x y)))
([x y & zs] (not (apply f x y zs)))))
Run Code Online (Sandbox Code Playgroud)
然而,它们的含义非常不同 - not对值进行操作,并返回值(true或false).complement操作并返回功能.
这可能看起来像一个实现细节,但它在表达您的内涵时非常重要 - 使用complement非常清楚您正在创建一个新函数,而not主要用于条件检查.