3

I have been learning Clojure and lately I have been using the REPL as a comand line calculator, my 'workflow' would be greatly improved if it were possible to pass arguments to the Clojure REPL and get the output, does anyone know how to do that?

Clarification: For example I would like to execute lein "(+ 2 2)" and have it return 4

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

5 Answers5

2

lein (Leiningen) is the wrong tool for this, other than starting up a REPL. If you really want a command line interface to some Clojure program, that's possible too, but requires you to compile it to a jar and execute it, cf. this article on building CLI clojure apps.

Thumbnail
  • 13,293
  • 2
  • 29
  • 37
schaueho
  • 3,419
  • 1
  • 21
  • 32
2

grenchman creates a repl, and each command line invocation gets a result from that repl, this is likely what you want.

noisesmith
  • 20,076
  • 2
  • 41
  • 49
0
# as bash variable
{ echo "$clj-expressions"; cat - ; } | lein repl

# as file
{ cat ./script.clj;        cat - ; } | lein repl

Lucky for us, lein repl is just a plain-old unix process

The idea here is to send your commands to the repl's stdin but ensure that the current terminal's stdin is connected afterwards.

Thanks to Jonathan Leffler for this one. His answer here solved this.

To collect output, you can always spit something out as part of the script you run.

Community
  • 1
  • 1
willscripted
  • 1,438
  • 1
  • 15
  • 25
-1

Anything you def is available at the REPL.

=> (def ten 10)
...
=> (defn fact [n] (apply * (range 1 (inc n))))
...
=> (fact ten)
 3628800
=>
Thumbnail
  • 13,293
  • 2
  • 29
  • 37
-1

This is exactly how REPL works - you write some expression and press Enter, got expression result back.

→  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=> 
edbond
  • 3,921
  • 19
  • 26
  • I added some clarification to my post. What I was looking to do is pass "(+ 21 21)" too lein and have it return 42. – xanth Jun 05 '14 at 11:49
  • see http://charsequence.blogspot.com/2012/04/scripting-clojure-with-leiningen-2.html – edbond Jun 05 '14 at 13:58