Clojure 命名空间管理 - 有没有办法保存和恢复 clojure repl 命名空间、导入等的状态?
Clojure 有大量函数/宏用于处理命名空间和 java 包导入。据我(有限)理解,命名空间的设置可以被视为 clojure 进程 (repl) 中的状态。
在 REPL 会话中迭代工作时,尤其是(重新)加载源文件时,我发现很容易感到困惑 - 通常当我在命名空间配置中犯下错误或语法错误时。其他时候,我想尝试重构命名空间/别名/引用过滤器,但在不重新启动 REPL 的情况下无法轻松退出现有命名空间状态。
例如,我希望能够检查命名空间配置 - 例如在 repl 加载代码主体之后 - 然后在尝试了在 REPL 导入的一些库后返回到“干净的状态”,以便我可以立即测试一个源文件,该文件导入该库中方法的过滤子集作为 ns 宏的一部分。
人们可以推荐保存和恢复命名空间配置的方法吗?
Clojure has a large number functions/macros for working with namespaces and java package imports. To my (limited) understanding the set up of namespaces can be considered state in a clojure process (repl).
When working iteratively at a REPL session, especially when source files are (re)-loaded, I can find it easy to get confused - often when I make a mistake or syntax error in namespace configuration. Other times I want to try out refactoring namespaces/aliases/reference filters but can't easily back out of existing namespace state without restarting the REPL.
For example I would like to be able to checkpoint namespace configuration - such as after the main body of code is loaded at the repl - then get back to that "clean-slate" after trying out some library imported at the REPL so that I can immediately test a source file that imports a filtered subset of methods in that library as part of the ns macro.
Can people recommend ways to save and restore namespace configuration?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我确信这有问题,因为我刚刚写它是为了回答这个问题,但我肯定会在我的项目中使用它。只需:导入它(将它放在项目中自己的文件中)并自由使用它。
首先,保存您的世界状态(您想要返回的世界),如下所示:
然后做任何您想做的事情 - 实验。当您准备好回到以前的状态时:
您应该可以开始了!
(希望这有效!正在为我工作 - 如果有问题请告诉我。我确信还有更好的方法可以做到这一点,但这有效,这就是我今晚所取得的进展。我确信我'会修改的。)
I'm sure there's something wrong with this, as I just wrote it in answer to this question, but I see myself using this in my projects, for sure. Just :import it (have it in its own file in your project) and use it liberally.
First, save a the state of your world (the one you want to go back to) like this:
Then do whatever you want–experiment. When you're ready to go back to your former state:
And you should be good to go!
(Hope this works! Was working for me–please let me know if there's a problem. I'm sure there's a better way to do this, too, but this works and it's how far I got tonight. I'm sure I'll revise.)
这并不总是有效。您可以使用 ns-unmap 从命名空间中删除变量,但其他代码片段可能仍保留对这些定义的引用。
Clojure 因为基于 JVM,所以没有像某些 Common Lisp 或 Scheme 实现那样的“内存映像”概念。
This won't always work. You can remove Vars from a namespace with
ns-unmap
, but other pieces of code may still hold references to those definitions.Clojure, because it is based on the JVM, has no concept of a "memory image" like some Common Lisp or Scheme implementations.
DMTCP 可能会以一种笨拙的方式完成这项工作。 Google 的 DMTCP:分布式多线程检查点。我用它来检查交互式 OCaml 程序。
DMTCP might do the job in a clumsy way. Google on DMTCP: Distributed MultiThreaded CheckPointing. I use it to checkpoint interactive OCaml programs.