用Leiningen创建第一个Clojure工程三

来源:互联网 发布:php 流媒体播放 编辑:程序博客网 时间:2024/04/29 17:05

现在来看一下前面译文中碰到的代码:

第一个是nil, 和Java的null值相同,和false一样可以表达条件判断不为true的情况。下面是解释:

nilnil is a possible value of any data type in Clojure. nil has the same value as Java null. The Clojure conditional system is based around nil and false, with nil and false representing the values of logical falsity in conditional tests - anything else is logical truth. In addition, nil is used as the end-of-sequence sentinel value in the sequence protocol.

因为之前main函数代码没有使用参数,这里修改一下代码,演示如何使用参数:

(ns project1.core)(defn -main  "I don't do a whole lot."  [& args]  (println "Hello, World!" )  (println args)  (println (count args)))

在REPL会话中运行:

user=> (require 'project1.core)niluser=> (project1.core/-main "b")Hello, World!(b)1nil
解释:

[  ] 方括号是指创建一个vector(数组)

& args指的是数组元素可变

(println args) 打印所有输入参数, args是一个list,包含了所有参数

(count args) 计算参数数目


现在看看如何不使用REPL会话运行程序, 直接输入下面的命令:

lein run -m project1.core "a" Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar Hello, World!(a)1

会自动找到project1.core namespace下面的-main函数并执行。



原创粉丝点击