Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Command line 将表达式作为参数传递给Clojure REPL_Command Line_Clojure - Fatal编程技术网

Command line 将表达式作为参数传递给Clojure REPL

Command line 将表达式作为参数传递给Clojure REPL,command-line,clojure,Command Line,Clojure,我一直在学习Clojure,最近我将REPL用作comand line计算器,如果可以将参数传递给Clojure REPL并获得输出,我的“工作流程”将大大改进,有人知道如何做到这一点吗 澄清:例如,我想执行lein“(+2)”并让它返回4 ~  lein "(+ 2 2)" '(+ 2 2)' is not a task. See 'lein help'. REPL提供您所需的任何内容 => (def ten 10) ... => (defn fact [n] (apply *

我一直在学习Clojure,最近我将REPL用作comand line计算器,如果可以将参数传递给Clojure REPL并获得输出,我的“工作流程”将大大改进,有人知道如何做到这一点吗

澄清:例如,我想执行lein“(+2)”并让它返回4

~  lein "(+ 2 2)"
'(+ 2 2)' is not a task. See 'lein help'.

REPL提供您所需的任何内容

=> (def ten 10)
...
=> (defn fact [n] (apply * (range 1 (inc n))))
...
=> (fact ten)
 3628800
=>

这正是REPL的工作原理-您编写一些表达式,然后按Enter键,返回表达式结果

→  lein repl
nREPL server started on port 59650 on host 127.0.0.1
REPL-y 0.3.0
Clojure 1.5.1
    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=> (+ 42 42)
84
user=> 
除了启动REPL之外,
lein
(Leiningen)是错误的工具。如果您真的想要某个Clojure程序的命令行接口,这也是可能的,但需要您将其编译成jar并执行,cf..

创建一个repl,并且每个命令行调用都会从该repl获得一个结果,这很可能是您想要的

# as bash variable
{ echo "$clj-expressions"; cat - ; } | lein repl

# as file
{ cat ./script.clj;        cat - ; } | lein repl
幸运的是,
lein repl
只是一个普通的旧unix进程

这里的想法是将您的命令发送到repl的stdin,但确保当前终端的stdin随后已连接

感谢乔纳森·莱弗勒的这一次。解决了这个问题


要收集输出,您可以随时在运行的脚本中输出一些内容。

我在帖子中添加了一些说明。我想做的是把“(+21)”也传给莱恩,让它返回42。谢谢,这正是我想要的。