Clojure:生成包含带有持久列表的 clojure 中断的文件
我在这里问了一个相关的问题: Clojure:如何将 clojure 代码转换为可计算的字符串?它大部分有效,但列表被转换为原始括号,这失败了
答案很好,但我意识到这并不完全是我所需要的。我简化了 stackoverflow 的示例,但我不仅仅是写出数据,我还尝试写出函数定义和其他包含列表结构的内容。这是一个简单的例子(选自上一个问题)。
我想生成一个包含以下功能的文件:
(defn aaa []
(fff :update {:bbb "bbb" :xxx [1 2 3] :yyy (3 5 7)}))
:update 之后的所有内容都是我在写入文件时可以访问的结构,因此我对其调用 str 并且它会以该状态出现。这很好,但是当我在这个生成的函数上加载文件时,列表会尝试将 3 作为函数调用(因为它是列表中的第一个元素)。
所以我想要一个包含我的函数定义的文件,然后我可以调用 load-file 并调用其中定义的函数。我怎样才能用关联的数据写出这个函数,以便我可以将它加载回来,而无需 clojure 认为过去的列表现在是函数调用?
I asked a related question here: Clojure: How do I turn clojure code into a string that is evaluatable? It mostly works but lists are translated to raw parens, which fails
The answer was great but I realized that is not exactly what I needed. I simplified the example for stackoverflow, but I am not just writing out datum, I am trying to write out function definitions and other things which contain structures that contain lists. So here is a simple example (co-opted from the last question).
I want to generate a file which contains the function:
(defn aaa []
(fff :update {:bbb "bbb" :xxx [1 2 3] :yyy (3 5 7)}))
Everything after the :update is a structure I have access to when writing the file, so I call str on it and it emerges in that state. This is fine, but the list, when I load-file on this generated function, tries to call 3 as a function (as it is the first element in the list).
So I want a file which contains my function definition that I can then call load-file and call the functions defined in it. How can I write out this function with associated data so that I can load it back in without clojure thinking what used to be lists are now function calls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在获取字符串表示之前,您需要
引用
结构:其中
foo
是结构。三点附加说明:
遍历代码来引用所有列表/seq 根本不起作用,因为顶级
(defn ...)
形式也会被引用;列表并不是唯一可能有问题的类型 - 符号是另一种类型(
+
与#
) ;而不是使用
(str foo)
(即使已经引用了foo
),您可能想要打印出引用的foo
-- 或者更确切地说,内部包含带引号的foo
的整个代码块 -- 使用pr
/prn
.最后一点值得简短讨论。
pr
明确承诺如果*print-readously*
为true
则生成可读表示,而str
仅生成这样的内容Clojure 复合数据结构“偶然”(实现的)的表示并且仅当*print-readically*
为true
时:上述行为是由于
clojure.lang.RT/printString
(这是 Clojure 数据结构最终委托其toString
需求的方法)使用clojure.lang.RT/print< /code>,它又根据
*print-readously*
的值选择输出格式。即使将
*print-readically*
绑定到true
,str
也可能会产生不适合clojure.lang.Reader
的输出的消耗:例如(str "asdf")
只是"asdf"
,而可读的表示是"\"asdf\""
。使用(with-out-str (pr foo))
获取包含foo
表示的字符串对象,如果*print-readously* 为
true
。You need to
quote
the structure prior to obtaining the string representation:where
foo
is the structure.Three additional remarks:
traversing the code to quote all lists / seqs would not do at all, since the top-level
(defn ...)
form would also get quoted;lists are not the only potentially problematic type -- symbols are another one (
+
vs.#<core$_PLUS_ clojure.core$_PLUS_@451ef443>
);rather than using
(str foo)
(even withfoo
already quoted), you'll probably want to print out the quotedfoo
-- or rather the entire code block with the quotedfoo
inside -- usingpr
/prn
.The last point warrants a short discussion.
pr
explicitly promises to produce a readable representation if*print-readably*
istrue
, whereasstr
only produces such a representation for Clojure's compound data structures "by accident" (of the implementation) and still only if*print-readably*
istrue
:The above behaviour is due to
clojure.lang.RT/printString
's (that's the method Clojure's data structures ultimately delegate theirtoString
needs to) use ofclojure.lang.RT/print
, which in turn chooses output format depending on the value of*print-readably*
.Even with
*print-readably*
bound totrue
,str
may produce output inappropriate forclojure.lang.Reader
's consumption: e.g.(str "asdf")
is just"asdf"
, while the readable representation is"\"asdf\""
. Use(with-out-str (pr foo))
to obtain a string object containing the representation offoo
, guaranteed readable if*print-readably*
istrue
.试试这个吧...
Try this instead...
将其包装在对
quote
的调用中以读取它而不对其进行评估。Wrap it in a call to
quote
to read it without evaluating it.