clojure-repl的使用套路

clojure 提倡交互式开发,交互离不开REPL. 下面是介绍repl的使用过程git

本文介绍所需如下 
1 任意编辑器
2leingithub

使用lein模板新建一个clojure项目

在命令行下输入json

$ lein new repl-test

lein会使用默认模板建立clojure项目 目录以下bash

repl-test/
├── CHANGELOG.md
├── LICENSE
├── README.md
├── doc
│   └── intro.md
├── project.clj
├── resources
├── src
│   └── repl_test
│       └── core.clj
└── test
    └── repl_test
        └── core_test.clj

core.clj中内容编辑器

(ns repl-test.core)

(defn foo
  "I don't do a whole lot."
  [x]
  (println x "Hello, World!"))

进入项目跟目录 启动repl

$ cd repl-test
$ lein repl

看到 user=>说明启动成功了函数

我想调用下core.clj中的foo函数ui

输入 (foo "diqye") 会报错  foo不在当前环境中   由于当前环境不在repl-test.core命名空间中spa

切换命名空间并require

user=> (in-ns 'repl-test.core)

看到 repl-test.core=>  就切换成功了
require命令行

repl-test.core=> (require 'repl-test.core)

执行 (foo "diqye") 会看到
diqye Hello, World!code

修改core.clj文件

core.clj

(ns repl-test.core)

(defn foo
  "I don't do a whole lot."
  [x]
  (println x "hello, clojure!"))

想要运行修改后的 foo须要load-file

repl-test.core=> (load-file "src/repl_test/core.clj")
#'repl-test.core/foo
repl-test.core=> (foo "diqye")
diqye hello, clojure!
nil
repl-test.core=>

不重启repl 增长第三方依赖 data.json

这个没有找到合适的方法去作,目前能够经过一个 库去作 https://github.com/clojure/tools.namespace

user=> (require '[clojure.tools.namespace.repl :refer [refresh]])

user=>(refresh) 会自动检测项目文件变化并加载

可是并不会自动加载project.clj新增的依赖项,只能重启repl

相关文章
相关标签/搜索