我最近认为我在clojure.spec
中遇到了一个bug,因为它在我将类似(fn [x] x)
或#(identity %)
的lambda表达式放入即将线程宏(不先将其包裹在一个额外的圆括号中,如((fn [x] x))
)时抛出异常。这是我多年来在Clojure中犯的最常见的重复“错误”,我听说过其他人也对此有误解。我这个“错误”是基于我对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