Clojure的REPL使用概要

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/iceyung/article/details/85954046

推荐:写给黄淮
推荐语:看过很多大神的博客,都是充满各种人文气息,想当初自己也是满怀激情,充满梦想,不管如何,享受生活,保持进步。


学习一门语言没有什么特殊的技巧,唯一的方法就是多写多练,掌握好语言的练习方式,哪怕有具体的知识点会遗忘也不必害怕,翻看之前的笔记,各种既定的函数与关键字多用几次也就熟悉了。

REPL使用之前

REPL是Clojure的重要的交互性工具,是“read-eval-print loop”,读取-运算-打印循环1
Clojure是运行在Java虚拟机之上的,所以需要首先安装JDK,相信JDK的安装就不用多说了吧,应该都已经掌握2
在Clojure初始中已经介绍过Leiningen来运行,这里给出一个详细的过程,可以参照该博文完成Leiningen的安装3,注意直接运行bat可能下载不成功,可以根据博文介绍,先下载zip文件,在更改bat中的leiningen-2.7.1-standalone.jar网络地址为本地地址,在此点击bat安装即可。

REPL简单使用

首先进入命令行窗口,输入lein repl:

nREPL server started on port 4898 on host 127.0.0.1 - nrepl://127.0.0.1:4898
REPL-y 0.4.3, nREPL 0.5.3
Clojure 1.8.0
Java HotSpot(TM) 64-Bit Server VM 1.8.0_131-b11
    Docs: (doc function-name-here)
          (find-doc "part-of-name-here")
  Source: (source function-name-here)
 Javadoc: (javadoc java-object-or-class-here)
    Exit: Control+D or (exit) or (quit)
 Results: Stored in vars *1, *2, *3, an exception in *e

user=>

出现上方显示的内容,在user=>后我们可以输入我们需要编写的语句,例如最简单的(+ 1 1)

user=> (+ 1 1)
;;-> 2

需要注意当我们的括号没有关闭时,可以换行输入:

user=> (+ 1
  #_=> 1)
;;-> 2

退出repl,输入exit即可:

user=> exit
;;->Bye for now!

注意:;;->为我标识控制台输出的内容,控制台中并不显示,在Clojure中;为注释符。user为默认的命名空间。

  • 创建一个项目
lein new helloclojure
;;->Generating a project called helloclojure based on the 'default' template.
;;->The default template is intended for library projects, not applications.
;;->To see other templates (app, plugin, etc), try `lein help new`.

查看当前目录可以看到:
在这里插入图片描述
在src目录中可以看到core.clj代码,为简单的hello world代码。

  • 使用repl
    之前说过repl是交互性工具,我们在刚刚创建的项目根目录下打开core.clj中的函数可以么,答案是肯定的,使用repl是可以进行项目的调试和运行的,这也是其强大的地方。
    首先修改project.clj项目文件,增加[clj-http “2.0.0”],再次启动repl,参见引文(该系列比较详细介绍了repl的使用)4:
(defproject helloclojure "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
            :url "https://www.eclipse.org/legal/epl-2.0/"}
  :dependencies [[org.clojure/clojure "1.9.0"]
  				[clj-http "2.0.0"]]
  :repl-options {:init-ns helloclojure.core})

更改过的项目文件,再次运行repl会帮我们自动下载引用的库,运行完成后直接进入了helloclojure.core的命名空间:

helloclojure.core=>

输入:

helloclojure.core=> (foo "clojure")
;;-> clojure Hello, World!
  • 输出为jar
    在ns里面添加了一个属性(:gen-class),通过命令可以将Clojure打包成jar包,详细见引文5
lein do clean, uberjar
;;-> 执行jar
java -jar target/helloclojure-0.1.0-SNAPSHOT-standalone.jar
;;->输出main函数中的数据
;;->clojure中的main函数:
(defn -main
  [& args]
  (println "Hello, World!"))

基于IDE的项目编写,在后续有机会会补上,大家也可以先参考该引文:6


  1. Living Clojure(中文版):中国电力出版社 ↩︎

  2. JDK安装:https://blog.csdn.net/zhys0902/article/details/79499329 ↩︎

  3. Leiningen安装使用:https://www.cnblogs.com/pekkle/p/6901764.html ↩︎

  4. repl使用:https://blog.csdn.net/csfreebird/article/details/8438894 ↩︎

  5. Clojure打包jar:https://blog.csdn.net/csfreebird/article/details/8440326 ↩︎

  6. Clojure IDE环境搭建:https://blog.csdn.net/zhxdick/article/details/50901950 ↩︎

猜你喜欢

转载自blog.csdn.net/iceyung/article/details/85954046