clojure-eval
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseClojure REPL Evaluation
Clojure REPL 评估
When to Use This Skill
何时使用该工具
Use this skill when you need to:
- Verify that edited Clojure files compile and load correctly
- Test function behavior interactively
- Check the current state of the REPL
- Debug code by evaluating expressions
- Require or load namespaces for testing
- Validate that code changes work before committing
当你需要以下操作时,可使用该工具:
- 验证编辑后的Clojure文件是否能正确编译和加载
- 交互式测试函数行为
- 检查REPL的当前状态
- 通过评估表达式调试代码
- 为测试引入或加载命名空间
- 在提交前验证代码更改是否生效
How It Works
工作原理
The command evaluates Clojure code against an nREPL server. Session state persists between evaluations, so you can require a namespace in one evaluation and use it in subsequent calls. Each host:port combination maintains its own session file.
clj-nrepl-evalclj-nrepl-evalInstructions
使用说明
0. Discover and select nREPL server
0. 发现并选择nREPL服务器
First, discover what nREPL servers are running in the current directory:
bash
clj-nrepl-eval --discover-portsThis will show all nREPL servers (Clojure, Babashka, shadow-cljs, etc.) running in the current project directory.
Then use the AskUserQuestion tool:
-
If ports are discovered: Prompt user to select which nREPL port to use:
- question: "Which nREPL port would you like to use?"
- header: "nREPL Port"
- options: Present each discovered port as an option with:
- label: The port number
- description: The server type and status (e.g., "Clojure nREPL server in current directory")
- Include up to 4 discovered ports as options
- The user can select "Other" to enter a custom port number
-
If no ports are discovered: Prompt user how to start an nREPL server:
- question: "No nREPL servers found. How would you like to start one?"
- header: "Start nREPL"
- options:
- label: "deps.edn alias", description: "Find and use an nREPL alias in deps.edn"
- label: "Leiningen", description: "Start nREPL using 'lein repl'"
- The user can select "Other" for alternative methods or if they already have a server running on a specific port
IMPORTANT: IF you start a REPL do not supply a port let the nREPL start and return the port that it was started on.
首先,发现当前目录中运行的所有nREPL服务器:
bash
clj-nrepl-eval --discover-ports这会显示当前项目目录中运行的所有nREPL服务器(包括Clojure、Babashka、shadow-cljs等)。
然后使用AskUserQuestion工具:
-
**如果发现了端口:**提示用户选择要使用的nREPL端口:
- 问题: "你想使用哪个nREPL端口?"
- 标题: "nREPL端口"
- 选项: 将每个发现的端口作为选项展示,包含:
- 标签: 端口号
- 描述: 服务器类型和状态(例如:"当前目录中的Clojure nREPL服务器")
- 最多展示4个发现的端口作为选项
- 用户可以选择"其他"来输入自定义端口号
-
**如果未发现端口:**提示用户如何启动nREPL服务器:
- 问题: "未找到nREPL服务器。你想如何启动一个?"
- 标题: "启动nREPL"
- 选项:
- 标签: "deps.edn 别名",描述: "在deps.edn中查找并使用nREPL别名"
- 标签: "Leiningen",描述: "使用'lein repl'启动nREPL"
- 用户可以选择"其他"来使用替代方法,或者如果他们已经在特定端口上运行了服务器
重要提示:如果你启动一个REPL,请不要指定端口,让nREPL自行启动并返回它所使用的端口。
1. Evaluate Clojure Code
1. 评估Clojure代码
Evaluation automatically connects to the given port
Use the flag to specify the port and pass your Clojure code.
-pRecommended: Pass code as a command-line argument:
bash
clj-nrepl-eval -p <PORT> "(+ 1 2 3)"For multiple expressions (single line):
bash
clj-nrepl-eval -p <PORT> "(def x 10) (+ x 20)"Alternative: Using heredoc (may require permission approval for multiline commands):
bash
clj-nrepl-eval -p <PORT> <<'EOF'
(def x 10)
(+ x 20)
EOFAlternative: Via stdin pipe:
bash
echo "(+ 1 2 3)" | clj-nrepl-eval -p <PORT>评估操作会自动连接到指定端口
使用标志指定端口并传入你的Clojure代码。
-p推荐方式:通过命令行参数传入代码:
bash
clj-nrepl-eval -p <PORT> "(+ 1 2 3)"多行表达式(单行传入):
bash
clj-nrepl-eval -p <PORT> "(def x 10) (+ x 20)"替代方式:使用here文档(可能需要权限审批才能使用多行命令):
bash
clj-nrepl-eval -p <PORT> <<'EOF'
(def x 10)
(+ x 20)
EOF替代方式:通过标准输入管道:
bash
echo "(+ 1 2 3)" | clj-nrepl-eval -p <PORT>2. Display nREPL Sessions
2. 查看nREPL会话
Discover all nREPL servers in current directory:
bash
clj-nrepl-eval --discover-portsShows all running nREPL servers in the current project directory, including their type (clj/bb/basilisp) and whether they match the current working directory.
Check previously connected sessions:
bash
clj-nrepl-eval --connected-portsShows only connections you have made before (appears after first evaluation on a port).
发现当前目录中的所有nREPL服务器:
bash
clj-nrepl-eval --discover-ports显示当前项目目录中运行的所有nREPL服务器,包括它们的类型(clj/bb/basilisp)以及是否匹配当前工作目录。
查看之前连接过的会话:
bash
clj-nrepl-eval --connected-ports仅显示你之前连接过的会话(在某个端口上首次评估后才会出现)。
3. Common Patterns
3. 常见使用模式
Require a namespace (always use :reload to pick up changes):
bash
clj-nrepl-eval -p <PORT> "(require '[my.namespace :as ns] :reload)"Test a function after requiring:
bash
clj-nrepl-eval -p <PORT> "(ns/my-function arg1 arg2)"Check if a file compiles:
bash
clj-nrepl-eval -p <PORT> "(require 'my.namespace :reload)"Multiple expressions:
bash
clj-nrepl-eval -p <PORT> "(def x 10) (* x 2) (+ x 5)"Complex multiline code (using heredoc):
bash
clj-nrepl-eval -p <PORT> <<'EOF'
(def x 10)
(* x 2)
(+ x 5)
EOFNote: Heredoc syntax may require permission approval.
With custom timeout (in milliseconds):
bash
clj-nrepl-eval -p <PORT> --timeout 5000 "(long-running-fn)"Reset the session (clears all state):
bash
clj-nrepl-eval -p <PORT> --reset-session
clj-nrepl-eval -p <PORT> --reset-session "(def x 1)"引入命名空间(始终使用:reload来获取最新更改):
bash
clj-nrepl-eval -p <PORT> "(require '[my.namespace :as ns] :reload)"引入命名空间后测试函数:
bash
clj-nrepl-eval -p <PORT> "(ns/my-function arg1 arg2)"检查文件是否可编译:
bash
clj-nrepl-eval -p <PORT> "(require 'my.namespace :reload)"多个表达式:
bash
clj-nrepl-eval -p <PORT> "(def x 10) (* x 2) (+ x 5)"复杂多行代码(使用here文档):
bash
clj-nrepl-eval -p <PORT> <<'EOF'
(def x 10)
(* x 2)
(+ x 5)
EOF注意:here文档语法可能需要权限审批。
自定义超时(毫秒):
bash
clj-nrepl-eval -p <PORT> --timeout 5000 "(long-running-fn)"重置会话(清除所有状态):
bash
clj-nrepl-eval -p <PORT> --reset-session
clj-nrepl-eval -p <PORT> --reset-session "(def x 1)"Available Options
可用选项
- - nREPL port (required)
-p, --port PORT - - nREPL host (default: 127.0.0.1)
-H, --host HOST - - Timeout (default: 120000 = 2 minutes)
-t, --timeout MILLISECONDS - - Reset the persistent nREPL session
-r, --reset-session - - List previously connected nREPL sessions
-c, --connected-ports - - Discover nREPL servers in current directory
-d, --discover-ports - - Show help message
-h, --help
- - nREPL端口(必填)
-p, --port PORT - - nREPL主机(默认:127.0.0.1)
-H, --host HOST - - 超时时间(默认:120000 = 2分钟)
-t, --timeout MILLISECONDS - - 重置持久化nREPL会话
-r, --reset-session - - 列出之前连接过的nREPL会话
-c, --connected-ports - - 发现当前目录中的nREPL服务器
-d, --discover-ports - - 显示帮助信息
-h, --help
Important Notes
重要注意事项
- Prefer command-line arguments: Pass code as quoted strings: - works with existing permissions
clj-nrepl-eval -p <PORT> "(+ 1 2 3)" - Heredoc for complex code: Use heredoc () for truly multiline code, but note it may require permission approval
<<'EOF' ... EOF - Sessions persist: State (vars, namespaces, loaded libraries) persists across invocations until the nREPL server restarts or is used
--reset-session - Automatic delimiter repair: The tool automatically repairs missing or mismatched parentheses
- Always use :reload: When requiring namespaces, use to pick up recent changes
:reload - Default timeout: 2 minutes (120000ms) - increase for long-running operations
- Input precedence: Command-line arguments take precedence over stdin
- 优先使用命令行参数: 将代码作为带引号的字符串传入:- 可在现有权限下正常工作
clj-nrepl-eval -p <PORT> "(+ 1 2 3)" - 复杂代码使用here文档: 对于真正的多行代码,使用here文档(),但请注意这可能需要权限审批
<<'EOF' ... EOF - 会话状态持久化: 状态(变量、命名空间、已加载的库)会在多次调用之间保持,直到nREPL服务器重启或使用
--reset-session - 自动修复分隔符: 该工具会自动修复缺失或不匹配的括号
- 始终使用:reload: 引入命名空间时,使用来获取最新更改
:reload - 默认超时时间: 2分钟(120000毫秒)- 对于长时间运行的操作,可增加该值
- 输入优先级: 命令行参数优先于标准输入
Typical Workflow
典型工作流程
- Discover nREPL servers:
clj-nrepl-eval --discover-ports - Use AskUserQuestion tool to prompt user to select a port
- Require namespace:
bash
clj-nrepl-eval -p <PORT> "(require '[my.ns :as ns] :reload)" - Test function:
bash
clj-nrepl-eval -p <PORT> "(ns/my-fn ...)" - Iterate: Make changes, re-require with , test again
:reload
- 发现nREPL服务器:
clj-nrepl-eval --discover-ports - 使用AskUserQuestion工具提示用户选择端口
- 引入命名空间:
bash
clj-nrepl-eval -p <PORT> "(require '[my.ns :as ns] :reload)" - 测试函数:
bash
clj-nrepl-eval -p <PORT> "(ns/my-fn ...)" - 迭代:修改代码,重新引入命名空间并使用,再次测试
:reload