【发布时间】:2016-02-02 17:36:26
【问题描述】:
目标:我正在尝试制作一个宏,该宏将以下内容作为输入:
(cb-chan (.readFile "/path/to/file" "utf8" _))
并作为输出返回如下:
(go (let [c (chan 1)
rep (.readFile "path/to/file" "utf8" (>? c)] ; >? is a function that I defined elsewhere that jams the result of a callback into a channel
rep
(<! c))))
请注意,原始输入中的_ 正在被特殊回调(在别处定义)替换。此回调将其结果塞入一个通道,然后在 go 块结束时检索并返回该通道。
尝试:
(defmacro cb-chan [func]
`(cljs.core.async.macros/go
(let [~'c (cljs.core.async/chan 1)]
~'rep (replace (quote {~'_ (cljs-async-patterns.core/>? ~'c) }) (quote ~func))
~'rep
(~'<! ~'c))))
结果:这失败了,因为 rep 只是最终成为一个文字的、未评估的列表。如果我能够在倒数第二行输入(eval rep) 而不仅仅是rep,我的问题将得到解决,但我不能,因为我正在使用ClojureScript(没有eval)。我该如何解决这个问题?
【问题讨论】:
-
你在这里想要什么还不清楚。字符串没有
.readFile方法,所以(.readFile "/path/to/file")永远不会是正确的。完全没有理由写(let [rep (foo)] rep x),而不仅仅是写(do (foo) x)。您可能只想写~(replace ...)而不是(replace ...),但很难确定,因为您想要的输入和输出并不完全有意义。
标签: clojure clojurescript