最近我发现自己在使用clojure.spec
时遇到了一个bug,因为它在将lambda(如(fn [x] x)
或#(identity %)
)放入 senator宏->
(需要先在圆括号中包裹)时抛出了一个异常。这是我多年来在Clojure中犯的最常见的重复“错误”,我也听说过别人对threading宏的这种误解。我这种情况下的“错误”是基于我对lambda是语言中基本“事物”或值的假设。但实际上,Clojure只是数据,主要是宏,每个宏以不同的方式处理其输入数据。
->
线程宏将lambda当作列表处理,而不是作为基本“函数值”。当执行(-> :hmm (fn [x] x))
时,将展开为 (fn :hmm [x] x)
,这显然不是预期效果。但是,它对非列表项(如符号、关键词等)有一个特殊方便的情况,它假定这可以被当作一个具有一个参数的函数处理,并在threading前后将其封装在圆括号中。这个特殊情况增加了价值,因为它解释了用户的 意图,避免了强迫用户在没有必要时添加圆括号。
我主张基本lambda形式(fn)
和#()
应该也容易像符号一样处理,通过解释用户的 意图。这样做将使threading宏更简单、更直观,可以移除影响许多用户的无关异常,并将语言推进到将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