Search code examples
clojure

Alternative to using the REPL in Clojure for quickly trying out stuff


In Python world, whenever I need to try something I'd just make a new file a.py and insert the code that I want to try, and run it. This works because of the shebang line #!/usr/bin/env python3 Which tells the os which interpreter to call for the file.

Is there an equivalent to this in clojure? I don't want to jump through all the hoops of running lein new app and specifying the main ns, everytime I just want to check out what a few lines of code does in a file.

Note that I already know about lein repl. And I tried to use that as the shebang #!/usr/bin/lein repl But this just brings up an error No :main namespace specified in project.clj.


Solution

  • If you have Clojure CLI tools installed, then you can use shebang scripts:

    test.clj:

    #!/usr/bin/env clj
    
    (def x 10)
    (println "x =" x)
    

    In terminal:

    chmod +x test.clj
    ./test.clj
    

    Output:

    x = 10