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

相關文章
相關標籤/搜索