Clojure
Online Clojure
main.clj runs as a script with clojure.main on Java 17, Clojure 1.12 and its core libraries. It starts from a class data archive, which cuts the usual JVM start-up time.
Try it aboveHow Clojure runs here
- Versions
- Clojure 1.12 on Java 17
- Run
java clojure.main main.clj- Limits per run
- 512 MB memory, 15 s of CPU time on half a core, 2 MB of output
- Time
- Up to 10 minutes a run (30 on a paid plan); waits 2 minutes for input (5 on a paid plan)
- Sandbox
- Each run gets its own Linux sandbox with no network access. Text files your program writes next to your code open as new tabs.
A Clojure program that reads input
This main.clj counts the words in a sentence with frequencies and sorts them by count, then by word:
(require '[clojure.string :as str]) (print "Enter a sentence: ") (flush) (let [words (re-seq #"\w+" (str/lower-case (read-line)))] (doseq [[word n] (sort-by (juxt (comp - val) key) (frequencies words))] (println word n)))
$ clojure main.clj Enter a sentence: The cat and the dog and THE bird the 3 and 2 bird 1 cat 1 dog 1
Reading input in Clojure
- (read-line) returns the next line as a string. Call (flush) after print so the prompt shows first; println flushes on its own here.
- parse-long and parse-double (Clojure 1.11 and later) turn text into numbers and return nil for bad input.
- Arguments from Run settings are in *command-line-args*.
What isn’t available here
- deps.edn and Leiningen dependencies: only Clojure, spec and the JDK are on the classpath. Java interop with JDK classes works.
- ClojureScript: this is Clojure on the JVM.
Errors you’ll see
- “Syntax error compiling at (main.clj:1:10). Unable to resolve symbol: squre in this context” A misspelt name, with the line and column.
- “Execution error (NumberFormatException) … For input string: "abc"” Long/parseLong got text that isn’t a number.
Questions
Which Clojure version is this?
Clojure 1.12 on Java 17, run with clojure.main.
Can I use libraries from Clojars?
No. Only Clojure’s core libraries and the JDK are available.
How do I read input in Clojure?
(read-line) for a line of text, (parse-long (read-line)) for a number. Type it in the terminal while the program runs.