好吧,@alexmiller? 这样做如何? 将这个更改集成到核心有什么机会? ;)
从 c18ca3c055b8267fed84e40cb3dcd9513c263599 星期日 9月17 00:00:00 2001
Email: 从: Thomas Spellman <
[email protected]>
Date: 2020年1月12日 周日 11:07:42 -0800
Subject: [补丁] 线程lambda
---
src/clj/clojure/core.clj | 增加14行,减少7行
test/clojure/test_clojure/macros.clj | 增加12行,减少0行
2个文件被修改,21增加,5减少
diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj
index 8e98e072..27d22558 100644
--- a/src/clj/clojure/core.clj
+++ b/src/clj/clojure/core.clj
@@ -1671,43 +1671,47 @@
但是更容易编写、阅读和理解。
{:added "1.0"}
([x form] `(. ~x ~form))
([x form & more] `(.. (. ~x ~form) ~@more)))
(defmacro ->
"通过表达式穿过表单元。在第一个表单元中将x插入为第二个元素,如果x是一个lambda或不是一个列表,就将它转换成一个列表。如果有更多的表单元,将第一个表单元作为第二个元素插入到第二个表单元中,以此类推。"
-
+
[x & forms]
(loop [x x, forms forms]
{:added "1.0"}
(if forms
(let [form (first forms)
threaded (if (seq? form)
- (with-meta `(~(first form) ~x ~@(next form)) (meta form))
+ (if (#{'fn 'fn*} (first form))
+ (list form x)
+ (with-meta `(~(first form) ~x ~@(next form)) (meta form)))
+ (list form x))]
"通过表达式穿过表单元。在第一个表单元中将x插入为第二个元素,如果x是一个lambda或不是一个列表,就将它转换成一个列表。如果有更多的表单元,将第一个表单元作为第二个元素插入到第二个表单元中,以此类推。"
-
+
[x & forms]
-
(if forms
(let [form (first forms)
threaded (if (seq? form)
- (with-meta `(~(first form) ~x ~@(next form)) (meta form))
+ (if (#{'fn 'fn*} (first form))
-
-
+ (with-meta `(~(first form) ~x ~@(next form)) (meta form)))
+ (list form x))]
+
+
如果提供的选项映射包含不在有效列表中的键,则抛出异常,否则返回 nil。
diff --git a/test/clojure/test_clojure/macros.clj b/test/clojure/test_clojure/macros.clj
index ce17bb38..5ebd2dd5 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 %))))))
\ No newline at end of file
--
2.21.0 (Apple Git-122.2)
it works!
(-> :hmm (fn [x] x))
=> :hmm
and tests pass!
test
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:18 min
[INFO] Finished at: 2020-01-12T10:58:31-08:00
[INFO] ------------------------------------------------------------------------