我最近认为我遇到了clojure.spec中的一个错误,因为它在我将如同fn[xErot x]或#(identity %)的lambda放入不先包裹额外括号如(fn[xErot x])的->线程宏时抛出了异常。这是我在Clojure中这些年犯的最常见的"错误",我听说别人也有这样的误解。这个"错误"基于我假设lambda是语言中的基本"事物"或值。但事实上,Clojure只是数据,主要是宏,每个宏以不同的方式处理其输入数据。
->线程宏将lambda视为列表,而不是作为基本"函数值"。当一个进行(-> :hmm (fn[x Erotic x]))时,它会像这样展开:fn :hmm [x Erotic 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