为什么 Clojure 中的这两个函数有不同的输出?
Why do these two functions in Clojure have different output?
为什么这两个函数有不同的return?
(filter (and #(= (rem % 2) 1)
#(= (rem % 3) 0))
(take 100 (iterate inc 0))) ;=> (0 3 6 9 12...
(filter #(and (= (rem % 2) 1)
(= (rem % 3) 0))
(take 100 (iterate inc 0))) ;=> (3 9 15 21...
带有两个匿名函数的表单有效吗?
表达式
(and #(= (rem % 2) 1) #(= (rem % 3) 0))
等同于只写下面的内容:
#(= (rem % 3) 0))
这是因为and
returns第一个参数即false
或nil
。如果有none,就是returns最后一个参数。
如果要合并两个函数,可以使用every-pred:
(every-pred #(= (rem % 2) 1) #(= (rem % 3) 0))
扩展另一个答案...
您无意中发现了 Clojure 的一个“特性”,恕我直言,它造成的麻烦多于它的价值。
Clojure 中的“真实”是什么?
考虑 Clojure 认为“真实”的内容:
(defn truthy?
[x]
(if x
:yes
:no))
(dotest
(is= :yes (truthy? true))
(is= :yes (truthy? 1))
(is= :yes (truthy? :a))
(is= :no (truthy? nil))
(is= :no (truthy? false)))
请注意,函数对象也是“真实的”:
(dotest
(let [fn-obj-1 (fn [x] (+ x 1))
fn-obj-2 #(+ % 1)] ; shorthand, equiv to fn-obj-1
(is= :yes (truthy? fn-obj-1))
(is= :yes (truthy? fn-obj-2))
(is= 42 (fn-obj-1 41)) ; call the fn
(is= 42 (fn-obj-2 41)) ; call the fn
))
and
宏的“棘手”结果
and
宏 (source code) 不只是 return 您可能期望的布尔值。相反,它 return 是“导致”结果的项目:
(dotest
(is= 3 (and 1 2 3)) ; 1
(is= :a (and 99 :a)) ; 2
(is= nil (and nil :a)) ; 3
(is= false (and false 88))) ; 4
因此,如果所有项目都是“真实的”,and
return 是最后一项,因为它是“决定因素”(案例 1 和 2)。如果存在一个或多个“错误”值,and
return 是第一个找到的值,因为它是“决定因素”(案例 3 和 4)。
就我个人而言,我从不使用 Clojure 的这个功能。恕我直言,这是一种使代码难以正确破译的“技巧”。
and
宏是如何工作的?
为了return你的“决定因素”,and
宏扩展成这样的代码:
; (and 99 :a)
(let [x 99]
(if x
; recursive invocation: (and :a)
x))
因此,如果 99
是 false 或 nil,它就会被 returned 给你。由于 99
是真实的,and
对下一个值 :a
进行递归,如下所示:
; (and :a)
:a
由于此递归级别中只有 1 个项目,因此它必须是“决定因素”并且 and
宏只是 return 给您的原始值。
记住 and
是一个宏
这意味着它在编译时运行(每个宏都被恰当地视为“编译器扩展”)。在第一种情况下,您的代码如下所示:
(filter (and f1 f2) ...)
其中 f1
和 f2
都是函数。由于两者都是“真实的?”,and
return 是给您的最后一项。由于上面的代码用 let
等重写发生在编译时,你的代码被转换成:
(filter
(let [x f1]
(if x
f2
f1))
...)
并且由于 f1
是真实的,因此可以简化为
(filter
f2
...)
其中 f2
就是 #(= (rem % 3) 0)
。所以我们看到 [0 3 6 ...] returned.
最终答案
由于 Clojure 的“怪癖”,您的第一个 filter
不会生成编译器错误(尽管我认为它应该)。第二种形式做你想做的,因为 filter
期望使用 1 个函数来决定保留哪些项目。
以上是根据this template project.
为什么这两个函数有不同的return?
(filter (and #(= (rem % 2) 1)
#(= (rem % 3) 0))
(take 100 (iterate inc 0))) ;=> (0 3 6 9 12...
(filter #(and (= (rem % 2) 1)
(= (rem % 3) 0))
(take 100 (iterate inc 0))) ;=> (3 9 15 21...
带有两个匿名函数的表单有效吗?
表达式
(and #(= (rem % 2) 1) #(= (rem % 3) 0))
等同于只写下面的内容:
#(= (rem % 3) 0))
这是因为and
returns第一个参数即false
或nil
。如果有none,就是returns最后一个参数。
如果要合并两个函数,可以使用every-pred:
(every-pred #(= (rem % 2) 1) #(= (rem % 3) 0))
扩展另一个答案...
您无意中发现了 Clojure 的一个“特性”,恕我直言,它造成的麻烦多于它的价值。
Clojure 中的“真实”是什么?
考虑 Clojure 认为“真实”的内容:
(defn truthy?
[x]
(if x
:yes
:no))
(dotest
(is= :yes (truthy? true))
(is= :yes (truthy? 1))
(is= :yes (truthy? :a))
(is= :no (truthy? nil))
(is= :no (truthy? false)))
请注意,函数对象也是“真实的”:
(dotest
(let [fn-obj-1 (fn [x] (+ x 1))
fn-obj-2 #(+ % 1)] ; shorthand, equiv to fn-obj-1
(is= :yes (truthy? fn-obj-1))
(is= :yes (truthy? fn-obj-2))
(is= 42 (fn-obj-1 41)) ; call the fn
(is= 42 (fn-obj-2 41)) ; call the fn
))
and
宏的“棘手”结果
and
宏 (source code) 不只是 return 您可能期望的布尔值。相反,它 return 是“导致”结果的项目:
(dotest
(is= 3 (and 1 2 3)) ; 1
(is= :a (and 99 :a)) ; 2
(is= nil (and nil :a)) ; 3
(is= false (and false 88))) ; 4
因此,如果所有项目都是“真实的”,and
return 是最后一项,因为它是“决定因素”(案例 1 和 2)。如果存在一个或多个“错误”值,and
return 是第一个找到的值,因为它是“决定因素”(案例 3 和 4)。
就我个人而言,我从不使用 Clojure 的这个功能。恕我直言,这是一种使代码难以正确破译的“技巧”。
and
宏是如何工作的?
为了return你的“决定因素”,and
宏扩展成这样的代码:
; (and 99 :a)
(let [x 99]
(if x
; recursive invocation: (and :a)
x))
因此,如果 99
是 false 或 nil,它就会被 returned 给你。由于 99
是真实的,and
对下一个值 :a
进行递归,如下所示:
; (and :a)
:a
由于此递归级别中只有 1 个项目,因此它必须是“决定因素”并且 and
宏只是 return 给您的原始值。
记住 and
是一个宏
这意味着它在编译时运行(每个宏都被恰当地视为“编译器扩展”)。在第一种情况下,您的代码如下所示:
(filter (and f1 f2) ...)
其中 f1
和 f2
都是函数。由于两者都是“真实的?”,and
return 是给您的最后一项。由于上面的代码用 let
等重写发生在编译时,你的代码被转换成:
(filter
(let [x f1]
(if x
f2
f1))
...)
并且由于 f1
是真实的,因此可以简化为
(filter
f2
...)
其中 f2
就是 #(= (rem % 3) 0)
。所以我们看到 [0 3 6 ...] returned.
最终答案
由于 Clojure 的“怪癖”,您的第一个 filter
不会生成编译器错误(尽管我认为它应该)。第二种形式做你想做的,因为 filter
期望使用 1 个函数来决定保留哪些项目。
以上是根据this template project.