Clojure:生成包含带有持久列表的 clojure 中断的文件

发布于 2025-01-04 20:45:27 字数 676 浏览 4 评论 0原文

我在这里问了一个相关的问题: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小帐篷 2025-01-11 20:45:27

在获取字符串表示之前,您需要引用结构:

(list 'quote foo)

其中foo是结构。

三点附加说明:

  1. 遍历代码来引用所有列表/seq 根本不起作用,因为顶级 (defn ...) 形式也会被引用;

  2. 列表并不是唯一可能有问题的类型 - 符号是另一种类型(+#) ;

  3. 而不是使用 (str foo) (即使已经引用了 foo),您可能想要打印出引用的 foo -- 或者更确切地说,内部包含带引号的 foo 的整个代码块 -- 使用 pr / prn.

最后一点值得简短讨论。 pr 明确承诺如果 *print-readously*true 则生成可读表示,而 str 仅生成这样的内容Clojure 复合数据结构“偶然”(实现的)的表示并且仅当 *print-readically*true

(str ["asdf"])
; => "[\"asdf\"]"

(binding [*print-readably* false]
  (str ["asdf"]))
; => "[asdf]"

:上述行为是由于clojure.lang.RT/printString(这是 Clojure 数据结构最终委托其 toString 需求的方法)使用 clojure.lang.RT/print< /code>,它又根据 *print-readously* 的值选择输出格式。

即使将 *print-readically* 绑定到 truestr 也可能会产生不适合 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:

(list 'quote foo)

where foo is the structure.

Three additional remarks:

  1. traversing the code to quote all lists / seqs would not do at all, since the top-level (defn ...) form would also get quoted;

  2. lists are not the only potentially problematic type -- symbols are another one (+ vs. #<core$_PLUS_ clojure.core$_PLUS_@451ef443>);

  3. rather than using (str foo) (even with foo already quoted), you'll probably want to print out the quoted foo -- or rather the entire code block with the quoted foo inside -- using pr / prn.

The last point warrants a short discussion. pr explicitly promises to produce a readable representation if *print-readably* is true, whereas str only produces such a representation for Clojure's compound data structures "by accident" (of the implementation) and still only if *print-readably* is true:

(str ["asdf"])
; => "[\"asdf\"]"

(binding [*print-readably* false]
  (str ["asdf"]))
; => "[asdf]"

The above behaviour is due to clojure.lang.RT/printString's (that's the method Clojure's data structures ultimately delegate their toString needs to) use of clojure.lang.RT/print, which in turn chooses output format depending on the value of *print-readably*.

Even with *print-readably* bound to true, str may produce output inappropriate for clojure.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 of foo, guaranteed readable if *print-readably* is true.

往昔成烟 2025-01-11 20:45:27

试试这个吧...

(defn aaa []
  (fff :update {:bbb "bbb" :xxx [1 2 3] :yyy (list 3 5 7)}))

Try this instead...

(defn aaa []
  (fff :update {:bbb "bbb" :xxx [1 2 3] :yyy (list 3 5 7)}))
空气里的味道 2025-01-11 20:45:27

将其包装在对 quote 的调用中以读取它而不对其进行评估。

Wrap it in a call to quote to read it without evaluating it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文