这个怎么样,@alexmiller?把这项更改放入核心的可能性有多大呢? (;)
从 c18ca3c055b8267fed84e40cb3dcd9513c263599 Mon Sep 17 00:00:00 2001
发件人:Thomas Spellman <
[email protected]>
日期:Sun, 12 Jan 2020 11:07:42 -0800
主题:[补丁] 线程lambda
---
src/clj/clojure/core.clj | 14 +++++++++-----
test/clojure/test_clojure/macros.clj | 12 ++++++++++++
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 ->
"通过expr遍历forms。在第一个form中将x作为第二个元素插入,如果它不是列表,则将其转换为列表。
- 如果存在更多forms,则将第一个form作为第二个元素插入第二个form等。
+ 如果存在更多forms,则将第一个form作为第二个元素插入第二个form等。
- [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))
- (list form x))
- )]
- (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)))
- )]
"通过expr遍历forms。在第一个form中将x作为第二个元素插入,如果它不是列表,则将其转换为列表。
- )]
+ (let [form (first forms)
- [x & forms]
+ threaded (if (seq? form)
+ (list form x)
- (if forms
- let [form (first forms)
- threaded (if (seq? form)
- (with-meta `(~(first form) ~x ~@(next form)) (meta form))
- (list form x))
+ (with-meta `(~(first form) ~x ~@(next form)) (meta form)))
+ )]
- (let [form (first forms)
- threaded (if (seq? form)
- (loop [threaded threaded, forms (next forms)]
+ (recur threaded (next forms)))
- (list form x)
- (with-meta `(~(first form) ~x ~@(next form)) (meta form)))
(def map)
(defn ^:private check-valid-options
"如果给定的选项映射包含不在有效列表中的键,则抛出异常,否则返回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
所有测试都通过了!
测试
[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] ------------------------------------------------------------------------