I have a basic Clojure script containing:
(def test
(future
(loop []
(println "Running")
(recur))))
However, if I execure the file with:
java -cp clojure-1.3.0.jar clojure.main test.clj
Then the screen fills with "Running". How can I change it so the future runs when I want it?
Note: I realise this will run forever, it's just an example of my problem.
A future that doesn't run immediately is just a function with no arguments.
So:
(defn test []
(println "Running")
(recur))
...Later...
(future (test))
Related
I've done some coding in clojure inside core.clj which have a -main method that can take 0 or more arguments:
(defn -main
"dequeue function"
[& args]
I'm loading this clj file with:
(load-file "src/exercise/core.clj")
And then i'm trying to learn how to call this within repl in order to develop core_test.clj (if there is any tips about how to develop this auto tests, please give me some hints as well). What I'm trying to do now is:
(-main "resources\\sample-input.json" "a")
But this is printing "Arguments 0" which is a message that I told the code to print just to see how many arguments are being passed with
(println "Arguments" (count *command-line-args*))
How am I suposed to do this?
Thanks!
i'm trying to learn how to call this within repl in order to develop core_test.clj
Usually you'd write other functions that are called from -main and test those rather than the app's entry point -main.
But you should be able to call -main like any other function. I have a src/sandbox/main.clj file:
(ns sandbox.main)
(defn -main [& args]
(prn args))
Starting a REPL in the project folder, I can call -main like this:
(use 'sandbox.main) ;; (load-file "src/sandbox/main.clj") also works
=> nil
(in-ns 'sandbox.main)
=> #object[clojure.lang.Namespace 0x67ccce04 "sandbox.main"]
(-main "some" "args")
;; ("some" "args")
=> nil
There's a key difference in my example though: it's printing -main's args binding; *command-line-args* is nil because you're not running the code from the command line with args, you're running it from a REPL.
Regardless, it's probably a better idea to use an existing library to work with CLI args rather than *command-line-args* directly.
I have created a Leiningen project for Exercise 2 from here. My code looks like this:
(ns random-quotes.core
(:require [clojure.string :as str])
(:gen-class))
(defn word-count [s]
(frequencies (str/split (first (str/split s #"\n")) #"\s")))
(def quote-url "http://www.braveclojure.com/random-quote")
(def total-word-count (atom {}))
(defn update-word-count []
(future
(swap! total-word-count
(partial merge-with +)
(word-count (slurp quote-url)))))
(defn quote-word-count [n]
(doseq [quote-future (doall (repeatedly n update-word-count))]
#quote-future)
#total-word-count)
(defn -main [n]
(doseq [entry (sort-by val (quote-word-count (bigdec n)))]
(println entry)))
All pretty straightforward. When I run, e.g., (-main 5) in lein repl, it runs, prints, and returns as expected. However, when I try lein run 5 instead, it runs and prints but never exits, so I am forced to use Ctrl+C to get my terminal back.
Any idea why this happens?
Clojure has a thread pool that it keeps running for use by the agents. Because those threads are still alive, the JVM can't tell that you're program is done. It's just sitting there waiting for the agents to exit. You can make them finish by calling (shutdown-agents) at the end of your program as described here. Futures use agents.
clojure.core/future-call calls an agent like this:
(let [f (binding-conveyor-fn f)
fut (.submit clojure.lang.Agent/soloExecutor ^Callable f)]
which actually starts your code running. You would not be the only one to voice some criticism of this, and we all hope a more elegant solution is found.
I would like to format my logs differently depending on whether my code is being run from a REPL or if I'm running the compiled jar.
Is there any simple way to do this? I was thinking maybe Leiningen leaves a trace somewhere when running the REPL.
(defn current-stack-trace []
(.getStackTrace (Thread/currentThread)))
(defn is-repl-stack-element [stack-element]
(and (= "clojure.main$repl" (.getClassName stack-element))
(= "doInvoke" (.getMethodName stack-element))))
(defn is-in-repl []
(some is-repl-stack-element (current-stack-trace)))
(defn my-log [msg]
(if (is-in-repl)
(prn (str "RUNNING IN REPL : " msg))
(prn msg)))
Is this an expected behaviour, or it's a problem with my Clojure installations (I'm checking it on Windows and Linux)?
I have a simple project, created with lein new app testfor. Here's the code of src/testfor/core.clj:
(ns testfor.core
(:gen-class))
(defn -main [& args]
(println "hello")
(for [x [1 2 3]]
(println x)))
When I run lein repl and invoke (-main) from the REPL, the output is this:
testfor.core=> (-main)
hello
1
2
3
But then I run the application with lein run, or lein uberjar and run the generated JAR file, the output is this:
$ lein run
hello
So, it somehow doesn't run the for loop.
Am I doing something wrong here?
I believe this is because for returns a lazy-sequence of elements - https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/for
In the example when you call the -main function from the repl, in this case the last expression in the function is returned from the function call (just like any other function call). The REPL sees the lazy-seq and attempts to print it, which requires realising the seq, which will evaluate the println statements.
In the example when you Leiningen calls the main method, it is like calling a main method on a Java class which has a void return type public static void main. Clojure will see it as lazy and nothing tries to realise the seq so the println expressions are never evaluated.
for is not a for loop like in Java. It creates a lazy sequence that depends on the different options you specify. See for documentation.
The important point in your case is the lazy word.
The repl will actually force the realisation of the lazy sequence, but when running with lein, this is not the case. In order to do what you want you need to force with either dorun or doall :
(defn -main [& args]
(println "hello")
(dorun (for [x [1 2 3]]
(println x))))
cuvier:tgo$ lein run
hello
1
2
3
The following program, when run from an überjar, exits at the end only when using the in-memory Datomic database; when connecting to the Datomic server, it hangs indefinitely rather than exiting the JVM:
(ns myns.example
(:use [datomic.api :only [db q] :as d])
(:gen-class))
;; WORKS: (def uri "datomic:mem://testdb")
(def uri "datomic:free://localhost:4334/testdb2")
(defn -main []
(println 1)
(when (d/create-database uri)
(d/connect uri))
(shutdown-agents)
(println 2))
Run as:
lein uberjar && java -cp target/myns-0.1.0-SNAPSHOT-standalone.jar myns.example
Outputs:
1
2
and hangs. It only hangs if the DB doesn't exist when the program starts.
Anyone know why, or how to fix? This is with both datomic-free-0.8.4020.26 and datomic-free-0.8.3941.
UPDATE -- the above program does actually terminate, but it takes a very long time (> 1 minute). I'd like to know why.
shutdown-agents takes up to one minute to complete (assuming no agents are running an action).
This is due to the way java.util.concurrent cached thread pools work.
Use datomic.api/shutdown
shutdown
function
Usage: (shutdown shutdown-clojure)
Shut down all peer
resources. This method should be called as part of clean shutdown of
a JVM process. Will release all Connections, and, if shutdown-clojure
is true, will release Clojure resources. Programs written in Clojure
can set shutdown-clojure to false if they manage Clojure resources
(e.g. agents) outside of Datomic; programs written in other JVM
languages should typically set shutdown-clojure to true.
Added in Datomic Clojure version 0.8.3861
(ns myns.example
(:require [datomic.api :as d])
(:gen-class))
(def uri "datomic:free://localhost:4334/testdb2")
(defn -main []
(d/create-database uri)
(let [conn (d/connect uri)]
(try
;; do something
(finally (d/shutdown true)))