我最近认为我在clojure.spec
中遇到了一个bug,因为它在我将lambda,如(fn [x] x)
或#(identity %)
放入没有先在额外的括号中包裹的->
宏时抛出了异常。((fn [x] x))
。这是我在Clojure years中犯的最常见的重复“错误”,我也听说其他人也有这种对宏的常见误解。在我这个例子中,“错误”基于我的假设,即lambda是语言中的一个基本“事物”或值。但实际上,Clojure只是数据,主要是宏,每个宏都以不同的方式处理其输入数据。
->
宏将lambda当作列表而不是基本“函数值”来对待。当进行(-> :hmm (fn [x] x))
时,它是这样展开的:(fn :hmm [x] x)
,这显然不是我们想要的。但是,它有一个针对非列表事物(如符号、关键字等)的特殊方便情况,它假设它是一个可以用一个参数表现行为的事物,并在宏绑定之前将其括号括起来。这个特殊情况的增加价值在于它解释了用户的意图,避免了在实际不需要时强制他们添加括号。
我坚称基本lambda表达式(fn)
和#()
应该并且很容易像符号一样被处理,通过解释用户的意图。这样做的好处是使宏更加简单和直观,可以删除影响众多用户的非必要异常,并使语言在处理lambda表达式时更接近核心“值”。最后一点是哲学性的,但在我看来对于使语言“正常工作”很重要,这样就符合新用户或非专家用户完成任务时的预期。
现有的将非列表形式包裹的特殊情况已经展示了提供一些解释用户意图的价值,但现在在这个特殊情况失踪,以及对于语言中最基础,“值”的两种最常见的形式之一的处理,即函数本身,这是不一致的。
你是否也被这种“错误”咬过?
如果您想与现有的->
宏并排尝试,这里有一个名为t->
的版本。
(defmacro t->
"Threads the expr through the forms. Inserts x as the
second item in the first form, making a list of it if it is a lambda or not a
list already. If there are more forms, inserts the first form as the
second item in second form, etc."
{:added "1.0"}
[x & forms]
(loop [x x, forms forms]
(if forms
(let [form (first forms)
threaded (if (and (seq? form) (not (#{'fn 'fn*} (first form))))
(with-meta `(~(first form) ~x ~@(next form)) (meta form))
(list form x))]
(recur threaded (next forms)))
x)))
我已为->
和->>
准备了一个补丁,该补丁可以应对这种情况,并且我没有想到会对其造成任何破坏性的影响。它保留了现有行为,同时增加了新功能。它通过了所有核心Clojure测试,但我还没有在我自己的代码之外对其进行测试。在它被考虑作为核心更改之前,这显然是必要的。
From 686831062a574486413022af31e8c7a07b78cd24 Mon Sep 17 00:00:00 2001
From: Thomas Spellman <[email protected]>
Date: Mon, 13 Jan 2020 20:39:45 -0800
Subject: [PATCH] thread macros
---
src/clj/clojure/core.clj | 12 ++++++------
test/clojure/test_clojure/macros.clj | 12 ++++++++++++
2 files changed, 18 insertions(+), 6 deletions(-)
diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj
index 8e98e072..fe43289b 100644
--- a/src/clj/clojure/core.clj
+++ b/src/clj/clojure/core.clj
@@ -1670,42 +1670,42 @@
(. (. System (getProperties)) (get \"os.name\"))
but is easier to write, read, and understand."
{:added "1.0"}
([x form] `(. ~x ~form))
([x form & more] `(.. (. ~x ~form) ~@more)))
(defmacro ->
- "Threads the expr through the forms. Inserts x as the
- second item in the first form, making a list of it if it is not a
+ "Threads the expr through the forms. Inserts x as the second item
+ in the first form, making a list of it if it is a lambda or not a
list already. If there are more forms, inserts the first form as the
second item in second form, etc."
{:added "1.0"}
[x & forms]
(loop [x x, forms forms]
(if forms
(let [form (first forms)
- threaded (if (seq? form)
+ threaded (if (and (seq? form) (not (#{'fn 'fn*} (first form))))
(with-meta `(~(first form) ~x ~@(next form)) (meta form))
(list form x))]
(recur threaded (next forms)))
x)))
(defmacro ->>
- "Threads the expr through the forms. Inserts x as the
- last item in the first form, making a list of it if it is not a
+ "Threads the expr through the forms. Inserts x as the last item
+ in the first form, making a list of it if it is a lambda or not a
list already. If there are more forms, inserts the first form as the
last item in second form, etc."
{:added "1.1"}
[x & forms]
(loop [x x, forms forms]
(if forms
(let [form (first forms)
- threaded (if (seq? form)
+ threaded (if (and (seq? form) (not (#{'fn 'fn*} (first form))))
(with-meta `(~(first form) ~@(next form) ~x) (meta form))
(list form x))]
(recur threaded (next forms)))
x)))
(def map)
(defn ^:private check-valid-options
diff --git a/test/clojure/test_clojure/macros.clj b/test/clojure/test_clojure/macros.clj
index ce17bb38..9fb1fa9e 100644
--- a/test/clojure/test_clojure/macros.clj
+++ b/test/clojure/test_clojure/macros.clj
@@ -106,8 +106,20 @@
(is (nil? (loop []
(as-> 0 x
(when-not (zero? x)
(recur))))))
(is (nil? (loop [x nil] (some-> x recur))))
(is (nil? (loop [x nil] (some->> x recur))))
(is (= 0 (loop [x 0] (cond-> x false recur))))
(is (= 0 (loop [x 0] (cond->> x false recur)))))
+
+(deftest ->lambda-test
+ (is (= 'a (-> 'a ((fn [x] x)))))
+ (is (= 'a (-> 'a (fn [x] x))))
+ (is (= 'a (-> 'a #(identity %))))
+ (is (= 'a (-> 'a (#(identity %))))))
+
+(deftest ->>lambda-test
+ (is (= 'a (->> 'a ((fn [x] x)))))
+ (is (= 'a (->> 'a (fn [x] x))))
+ (is (= 'a (->> 'a #(identity %))))
+ (is (= 'a (->> 'a (#(identity %))))))
--
2.21.0 (Apple Git-122.2)
提交记录:https://clojure.atlassian.net/browse/CLJ-2553