gpt4 book ai didi

clojure - 在 Clojurescript 中使用 -?>?

转载 作者:行者123 更新时间:2023-12-01 05:27:38 26 4
gpt4 key购买 nike

在我的 Clojure 共享源代码中,我有以下内容(我厚颜无耻地偷走了):

(defmacro hey-str [name]
`(str "hey " ~name))

{:author "Laurent Petit (and others)"
:doc "Functions/macros variants of the ones that can be found in clojure.core
(note to other contrib members: feel free to add to this lib)"}

(defmacro- defnilsafe [docstring non-safe-name nil-safe-name]
`(defmacro ~nil-safe-name ~docstring
{:arglists '([~'x ~'form] [~'x ~'form ~'& ~'forms])}
([x# form#]
`(let [~'i# ~x#] (when-not (nil? ~'i#) (~'~non-safe-name ~'i# ~form#))))
([x# form# & more#]
`(~'~nil-safe-name (~'~nil-safe-name ~x# ~form#) ~@more#))))

(defnilsafe
"Same as clojure.core/-> but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation).
Examples :
(-?> \"foo\" .toUpperCase (.substring 1)) returns \"OO\"
(-?> nil .toUpperCase (.substring 1)) returns nil
"
-> -?>)

(defnilsafe
"Same as clojure.core/.. but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation).
Examples :
(.?. \"foo\" .toUpperCase (.substring 1)) returns \"OO\"
(.?. nil .toUpperCase (.substring 1)) returns nil
"
.. .?.)

(defnilsafe
"Same as clojure.core/->> but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation).
Examples :
(-?>> (range 5) (map inc)) returns (1 2 3 4 5)
(-?>> [] seq (map inc)) returns nil
"
->> -?>>)

在我的 Clojurescript 代码中,我有以下内容(我 :require-macros as c)
(def a nil)
(def b [])
(def c [{:a 23}])

(js/alert (c/hey-str "Stephen")) ;; should display "hey Stephen"
(js/alert (c/-?> a first :a)) ;; should display nil
(js/alert (c/-?> b first :a)) ;; should display nil
(js/alert (c/-?> c first :a)) ;; should display 23

不幸的是,当我编译时,我得到:
WARNING: Use of undeclared Var webstack.client/-?> at line 56 cljs-src/webstack/client.cljs
WARNING: Use of undeclared Var webstack.client/-?> at line 57 cljs-src/webstack/client.cljs
WARNING: Use of undeclared Var webstack.client/-?> at line 58 cljs-src/webstack/client.cljs

当我在浏览器中打开 javascript 时,我收到“嘿斯蒂芬”警报对话框,但是在“嘿斯蒂芬”上按“确定”后立即出现无处不在的“未捕获的类型错误:无法调用未定义的方法‘调用’”错误警报。果然,查看生成的 javascript 代码,我的 js/alert 变成了:
alert([cljs.core.str("hey "), cljs.core.str("Stephen")].join(""));
alert(webstack.client.__QMARK__GT_.call(null, webstack.client.__QMARK__GT_.call(null, webstack.client.a, cljs.core.first), "\ufdd0'a"));
alert(webstack.client.__QMARK__GT_.call(null, webstack.client.__QMARK__GT_.call(null, webstack.client.b, cljs.core.first), "\ufdd0'a"));
alert(webstack.client.__QMARK__GT_.call(null, webstack.client.__QMARK__GT_.call(null, webstack.client.c, cljs.core.first), "\ufdd0'a"))

很明显,我可以使用宏,但是 -?> (和相关)宏的编写方式会导致它们无法编译。我需要做什么才能使用这些 -?> .?.和“-?>>”宏?

最佳答案

-?> 使用 clojure 1.5.1 和 cljsbuild 0.3.0 对我有用,但前提是我使用 :use-macros 而不是 :require-macros。当我使用 :require-macros clojurescript 时,尝试将宏解析为本地命名空间中的 var,这是不正确的。我想你在 clojurescript 中发现了一个错误,你为什么不报告它?

(ns test.test
;(:use-macros [test.nilsafe :only [hey-str -?>]]) ;uncomment and everything works!
(:require-macros [test.nilsafe :as tn]))

(def a nil)
(def b [])
(def c [{:a 23}])

(.log js/console (tn/hey-str "Stephen")) ;; should display "hey Stephen"
(.log js/console (tn/-?> a first :a)) ;; should display nil
(.log js/console (tn/-?> b first :a)) ;; should display nil
(.log js/console (tn/-?> c first :a))

让我确定这是一个错误的事情是取消注释 :use-macros 将导致文件正确编译,即使我没有从变量中删除 tn/范围。

关于clojure - 在 Clojurescript 中使用 -?>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13021411/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com