我该如何编写一个Clojure REPL应用程序?

3
我希望能够以Clojure REPL的形式提供一个应用程序,即用户从终端运行并具有完整的Clojure REPL和一堆额外的功能,这些功能是作为Clojure函数包含在内的。
我希望整个应用程序都存在于一个独立的JAR文件中,并且不依赖于用户已经安装Clojure或知道如何安装Clojure或导入额外的库。只需在运行时出现整个应用程序。
是否有一种简单直接的方法来实现这一点?即重用现有的Clojure REPL代码?
1个回答

4

你只需要在自己的主函数中运行 clojure.main/repl

文档解释了你所拥有的入口点:

"Generic, reusable, read-eval-print loop. By default, reads from *in*,
writes to *out*, and prints exception summaries to *err*. If you use the
default :read hook, *in* must either be an instance of
LineNumberingPushbackReader or duplicate its behavior of both supporting
.unread and collapsing CR, LF, and CRLF into a single \\newline. Options
are sequential keyword-value pairs. Available options and their defaults:
   - :init, function of no arguments, initialization hook called with
     bindings for set!-able vars in place.
     default: #()
   - :need-prompt, function of no arguments, called before each
     read-eval-print except the first, the user will be prompted if it
     returns true.
     default: (if (instance? LineNumberingPushbackReader *in*)
                #(.atLineStart *in*)
                #(identity true))
   - :prompt, function of no arguments, prompts for more input.
     default: repl-prompt
   - :flush, function of no arguments, flushes output
     default: flush
   - :read, function of two arguments, reads from *in*:
       - returns its first argument to request a fresh prompt
         - depending on need-prompt, this may cause the repl to prompt
           before reading again
       - returns its second argument to request an exit from the repl
       - else returns the next object read from the input stream
     default: repl-read
   - :eval, function of one argument, returns the evaluation of its
     argument
     default: eval
   - :print, function of one argument, prints its argument to the output
     default: prn
   - :caught, function of one argument, a throwable, called when
     read, eval, or print throws an exception or error
     default: repl-caught"

这里常见的操作:

  • :init: 添加你自己的设置用于REPL。例如:use 一些库
  • :prompt: 显示不同于ns的“状态”
  • :print: 使用某种漂亮的打印机

值得一提的是,每个你构建的程序(例如uberjaring),包含 clojure.main(例如 clojure-$VERSION.jar)都可以从那个uberjar运行REPL,因此你可以从那里运行不同的 main

自我推广:如果你需要更多灵感,这些是我过去所做的“修改版”REPL:

  • REST-REPL:添加有状态的URL导航和工具,以消费JSON/XML API
  • WREPL:使用integrant实现基于模块的REPL组合

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接