Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 230915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:53:22+00:00 2026-05-11T19:53:22+00:00

I’m dabbling in clojure and am having a little trouble trying to determine the

  • 0

I’m dabbling in clojure and am having a little trouble trying to determine the clojure (and / or Lisp) equivalent of this common python idiom.

The idiom is that at the bottom of a python module there is often a bit of test code, and then a statement which runs the code, for example:

# mymodule.py
class MyClass(object):
    """Main logic / code for the library lives here"""
    pass

def _runTests():
    # Code which tests various aspects of MyClass...
    mc = MyClass() # etc...
    assert 2 + 2 == 4

if __name__ == '__main__': _runTests()

This is useful for simple, ad-hoc testing. One would normally use this module by writing from mymodule import MyClass, in which case _runTests() is never called, but with the snippet at the end, one can also run it by typing python mymodule.py directly from the command line.

Is there an equivalent idiom in Clojure (and/or common lisp)? I’m not after a full-blown unit testing library (well, I am, but not in this question), I’d just like to include some code in a module which will only be run under some circumstances, so I can have a quick way to run code I’ve been working on but still allow my file to be imported like a normal module / namespace.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:53:22+00:00Added an answer on May 11, 2026 at 7:53 pm

    It’s not idiomatic to run Clojure scripts over and over from the command line. The REPL is a better command line. Clojure being a Lisp, it’s common to fire up Clojure and leave the same instance running forever, and interact with it rather than restart it. You can change functions in the running instance one at a time, run them and poke them as needed. Escaping the tedious and slow traditional edit/compile/debug cycle is a great feature of Lisps.

    You can easily write functions to do things like run unit tests, and just call those functions from the REPL whenever you want to run them and ignore them otherwise. It’s common in Clojure to use clojure.contrib.test-is, add your test functions to your namespace, then use clojure.contrib.test-is/run-tests to run them all.

    Another good reason not to run Clojure from the commandline is that the startup time of the JVM can be prohibitive.

    If you really want to run a Clojure script from the command line, there are a bunch of ways you can do it. See the Clojure mailing list for some discussion.

    One way is to test for the presence of command line arguments. Given this foo.clj in the current directory:

    (ns foo)
    
    (defn hello [x] (println "Hello," x))
    
    (if *command-line-args*
      (hello "command line")
      (hello "REPL"))
    

    You’ll get different behavior depending how you start Clojure.

    $ java -cp ~/path/to/clojure.jar:. clojure.main foo.clj --
    Hello, command line
    $ java -cp ~/path/to/clojure.jar:. clojure.main
    Clojure 1.1.0-alpha-SNAPSHOT
    user=> (use 'foo)
    Hello, REPL
    nil
    user=>
    

    See src/clj/clojure/main.clj in the Clojure source if you want to see how this is working.

    Another way is to compile your code into .class files and invoke them from the Java command line. Given a source file foo.clj:

    (ns foo
      (:gen-class))
    
    (defn hello [x] (println "Hello," x))
    
    (defn -main [] (hello "command line"))
    

    Make a directory to store the compiled .class files; this defaults to ./classes. You must make this folder yourself, Clojure won’t create it. Also make sure you set $CLASSPATH to include ./classes and the directory with your source code; I’ll assume foo.clj is in the current directory. So from the command line:

    $ mkdir classes
    $ java -cp ~/path/to/clojure.jar:./classes:. clojure.main
    Clojure 1.1.0-alpha-SNAPSHOT
    user=> (compile 'foo)
    foo
    

    In the classes directory you will now have a bunch of .class files. To invoke your code from the command line (running the -main function by default):

    $ java -cp ~/path/to/clojure.jar:./classes foo
    Hello, command line.
    

    There’s a lot of information about compiling Clojure code on clojure.org.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm trying to create an if statement in PHP that prevents a single post
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.