Forcing Cake to reload functions from my .clj files - clojure

I'm new to Clojure, and after too much of my life have been already wasted on waiting for Leiningen to run my code, I'm trying to move to Cake. While Cake's persistent JVM loads up blazing fast - it presents a bigger problem - my functions are also persistent!
To demonstrate the problem, I've started a cake project(using cake new mess-up-with-cake), and wrote this in core.clj:
(ns mess-up-with-cake.core)
(defn main-function[]
(println "I'm in the main function")
)
(println "I'm in core.clj, not inside in any function")
And this is project.clj:
(defproject mess-up-with-cake "0.0.1-SNAPSHOT"
:description "TODO: add summary of your project"
:dependencies [[clojure "1.2.0"]])
(use 'mess-up-with-cake.core)
(deftask my-task
(println "I'm in my task")
(main-function)
)
When running it with cake my-task, I get:
I'm in core.clj, not inside in any function
I'm in my task
I'm in the main function
No surprise here.
Now, I've changed core.clj to this:
(ns mess-up-with-cake.core)
(defn main-function[]
(println "I'm in the main function")
(println "I've made a change in the main function")
)
(println "I'm in core.clj, not inside in any function")
(println "I've made a change outside the main function")
And when I run it, I get
I'm in core.clj, not inside in any function
I've made a change outside the main function
I'm in my task
I'm in the main function
core.clj was clearly reloaded, but the change I've made inside the main function was not printed! Only when I stop the JVM with cake kill and rerun it I get the desired result - but if I have to restart the JVM every time I change a function, I might as well go back to lein...
Any idea how to force cake to reload my functions(and only my functions - reloading the entire Clojure runtime + any libraries I'm using probably won't be much faster than restarting the JVM..)?

This may not directly answer your question, though I hope it helps:
It sounds like your workflow if you where using leiningen would be to run:
lein run
wait for the JVM to start up .... get bored ...
observe result
edit code
repeat
This is a very common pattern in most languages and it's occasionally used for Clojure development (and Cake is very helpful here). It is much more common for Clojure development to use a single instance of the project and connect the editor to that instance using nrepl (or Slime and Swank). Because most everyone leaves the project running while they do the development not many people feel this pain and so the solutions are not as good in my opinion. Cake has largely been merged into Leiningen and the future direction of the Cake project is not clear to me (I could very well be wrong on this point). Of the Clojureians I know, all of them have moved to Leiningen and connect to their project from an editor like Emacs or vim.
a common workflow is:
start Emacs
M-x nrepl-jack-in
Ctrl-c Crtl-l to reload all the namespace and all it's dependent namespaces (this is close to a solution to your problem)
hack, load, repeat ;-)
This workflow is not Emacs or VI specific, the same method is used from Eclipse and Intelij
re: " reloading the entire Clojure runtime + any libraries I'm using probably won't be much faster than restarting the JVM".
I find it to be no more than two seconds even with my larger projects

I used to struggle with the slow JVM startup speed as well, and had mixed success with Cake. You might want to take a look at the excellent autoexpect plugin for Leiningen, explained in some length in the author's blog post. Basically autoexpect reloads your code every time the working directory tree is updated (and evaluates any expect clause, reporting any test failures). Makes continuous testing a dream -- I sometimes have two shells going in Emacs -- one for the output of lein autoexpect, one for a connected REPL to send snippets of code to as the other poster is suggesting.
I like the continuous testing style so much I wrote a similar utility in Python for my non-Clojure development (described in this blog post) -- for Clojure, I use autoexpect.

Related

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.
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
You can do this with boot. See https://github.com/boot-clj/boot/wiki/Scripts
Also, the ClojureVerse has a thread on this at https://clojureverse.org/t/scripting-with-clj/1618/5 where some options are being discussed.
Still, you should be aware that such an approach is probably not very useful. On the one hand you quite often want to manage dependencies on the other there's the startup time of the JVM, in particular when you need to compile some clojure sources first. If you need to run your script a few times because you're trying out thing, the startup time quickly becomes are real obstacle.
As an idea, I usually keep a leiningen project around for quick experiments. Here, I can adjust the dependencies as needed and quickly fire up a REPL to tinker with ideas and try out things. For me, it is not uncommon to find this REPL up and running and an Emacs already connected to it.

Clojure Cursive noob load into repl and cannot run hello world

Exploring Clojure - I am working through Clojure For The Brave And True. I created the hello world (in this case, I'm a little teapot). I can run it from lein repl just as the book suggests.
The author of the book appears to be a big emacs fan. I am comfortable with Intellij Idea, so installed the Cursive plugin. I then:
Set up a repl "runner" as shown on the Cursive site.
Tried loading the hello world into the repl. It appears to have done
so.
Now what? I presumed after loading the contents of the editor, I could run it, but have not figured out how. Obviously, this is a very noob question.
The code in my editor follows.
(ns clojure-noob.core
(:gen-class))
(defn -main
"I don't do a whole lot ... yet."
[& args]
(println "I'm a little teapot!")
)
The repl window is divided into two panes. The upper pain shows it is connected to the local nREPL server. Then, after loading it states:
Loading src/clojure_noob/core.clj... done
In the lower pane, per comments, I tried two variations of run, both of which failed miserable as you can see below (it didn't surprise me the second failed for it was calling out run, not the function).
run -main
CompilerException java.lang.RuntimeException: Unable to resolve symbol: run in this context, compiling:(/tmp/form-init2302589649746976452.clj:1:4481)
=> #object[clojure_noob.core$_main 0x24d21d67 "clojure_noob.core$_main#24d21d67"]
run clojure-noob.core/-main
CompilerException java.lang.RuntimeException: Unable to resolve symbol: run in this context, compiling:(/tmp/form-init2302589649746976452.clj:1:4481)
=> #object[clojure_noob.core$_main 0x24d21d67 "clojure_noob.core$_main#24d21d67"]
ANSWER
Thanks to comments from #Carcigenicate, I was able to figure out what to do. The answer is:
Enter (clojure-noob.core/-main) into the lower pane
Position cursor to the right of the closing paren
Press enter.
This resulted in the following:
(clojure-noob.core/-main)
I'm a little teapot!
=> nil
Told you it was a noob question!
This will be much easier in the next version of Cursive - the EAP will hopefully be out later this week or next week. You'll have the IntelliJ-standard ways of running -main functions more easily accessible:
Of course, the Clojure Way is to use the REPL as you're discovering, so continuing to explore that way of programming is definitely to be encouraged!

Is there a good ClojureScript workflow?

I'm trying to use ClojureScript for a project. To be a happy developer, I need a quick feedback-loop: write some code and see it as soon as it's possible. I'd also love to use REPL in the context of the current page.
I'm using lein-cljsbuild and from the issues I'm having I think I failed to set it up properly.
First of all, compilation time for a small file is too big, even after "JWM warm-up" with :optimizations :simple:
Compiling "resources/public/js/cljs.js" from ["src-cljs"]...
Successfully compiled "resources/public/js/cljs.js" in 8.233018 seconds.
Compiling "resources/public/js/cljs.js" from ["src-cljs"]...
Successfully compiled "resources/public/js/cljs.js" in 5.522989 seconds.
Compiling "resources/public/js/cljs.js" from ["src-cljs"]...
Successfully compiled "resources/public/js/cljs.js" in 8.144354 seconds.
Second, from time to time I get stuff like this:
Compiling "resources/public/js/cljs.js" from ["src-cljs"]...
Compiling "resources/public/js/cljs.js" failed.
Exception in thread "main" java.lang.NullPointerException
at java.util.regex.Matcher.getTextLength(Matcher.java:1140)
at java.util.regex.Matcher.reset(Matcher.java:291)
at java.util.regex.Matcher.<init>(Matcher.java:211)
at java.util.regex.Pattern.matcher(Pattern.java:888)
at clj_stacktrace.utils$re_gsub.invoke(utils.clj:6)
# ... many similar useless lines
at clojure.lang.Var.applyTo(Var.java:532)
at clojure.main.main(main.java:37)
These stacktraces (most of the time related to syntax errors, as I understood from experimenting), are worse than not helpful: not only they provide zero clues about the error, they also stop automatic recompilation. After every such stacktrace I should do these things:
Figure out what's the problem, using a technique I call "pretend you're a parser". Thank God it's Lisp and the only thing I should do is to carefully count matching parentheses.
Press Cmd-C in the shell where the lein cljsbuild auto runs
Run lein cljsbuild auto again
Make some useless change, like inserting a newline, in the file, to trigger a new build
Wait for ~30 seconds until the file is compiled again, since the JVM has to "warm-up" again
In many cases at this point I get another stacktrace, after getting which I have to lather, rinse and repeat all the damned procedure
Third thing which I failed to understand is REPL. From the interwebs I've learned how to connect rlwrap lein trampoline cljsbuild repl-listen to the current page. However, every reload, navigation, and syntax error (god forbid you forget to use rlwrap and press an arrow key) in the console makes the REPL completely stuck. I have to restart it (~10 seconds) and reload the page after that, then start again trying stuff out in a friendly environment.
I think, probably someone has figured it all out and has a workflow that works. Please help me set it all up from the start, because I feel like a failed software engineer.
For me Catnip provides the best dev experience for ClojureScript at the moment.
Check out figwheel! It's like livereload on steroids.
Working with clojurescript and front-end development was never so much fun!
See for yourself (the demo is a bit outdated – it's even better now!)
It not just reloads the page when files are changed – it provides you interactive programming Bret Victor was talking about (I recommend you to watch that talk if you haven't)
I've tried inside Emacs this tool https://github.com/cemerick/austin and works very well as you can see in his demo and screencast.
Have a good development
Juan
When in development mode turn :optimizations to :none, it makes a huge difference it won't change the initial compile time but any additional changes will only take milliseconds typically.
I have the following in my project.clj
:cljsbuild {:builds [{;; Directories of interest:
:source-paths ["src"]
;; Compiler flags:
:compiler {;; Where to save the file:
:output-to "resources/public/js/app.js"
;; Where to put the output directory
:output-dir "resources/public/js/out"
;; Optimizations:
:optimizations :none
}}]}
compilation time for a small file is too big
Try :optimizations :whitespace. And use lein cljsbuild auto
Second, from time to time I get stuff like this: ......
I haven't met this problem before. It looks like a bug in cljsbuild. Try to change another version maybe. I'm using [lein-cljsbuild "1.0.2"] with [org.clojure/clojurescript "0.0-2156"]. The 1.0.3 doesn't work in my setup.
Third thing which I failed to understand is REPL
In your case the key point is to find a way reloading namespaces without restart REPL or reloading page.
I find load-file is good for this.
My workflow is:
First start REPL: rlwrap lein trampoline cljsbuild repl-listen
Make sure there is (repl/connect "http://localhost:9000/repl") in my code.
Open my page in browser.
Do some test (my-namespace/my-func a b c)
Edit my codes.
In my REPL, run (load-file "path-to-my-file.cljs")
Repeat 4
This is still a pretty annoying workflow. load-file is slow and fragile so sometimes I browser to test. I hope someone can provide a better answer.

expected usage of pallet from lein

I was reading about pallet here: http://twoguysarguing.wordpress.com/2010/11/01/starting-a-cluster-on-ec2-with-pallet/, as well as on the pallet site: http://palletops.com/. I'm still a little confused. The examples are arranged as if I'm expected to enter the code at a REPL.
But I think I'm missing something. Because I feel like (or I'm hoping) there should be some sort of lein support/integration, so that I can define some code to spin up a cluster, start the cluster, stop the cluster, deploy a war file, etc. via a series of lein commands.
I did find the following project on github, but it appears to be written to work with maven instead of lein: https://github.com/cemerick/clojure-web-deploy-conj.
And, this is very close to what I want, except that I want this tied into lein targets somehow: http://cemerick.com/2010/05/12/provisioning-administration-and-deployment-of-couchdb-java-tomcat/.
The Leiningen wiki refers to pallet/pallet-lein.
I don't have any experience with pallet, but it looks like the Leiningen plugin passes the first plugin argument to a function in the pallet.main namespace which appears to call into pallet.
So, the argument foo in lein pallet foo would be passed along to pallet.
So I found the following http://nakkaya.com/2010/02/25/writing-leiningen-plugins-101/, which describes the process for creating your own plugin. As it turns out it's fairly straightforward. Create a top level leiningen/ directory, give it a namespace name, and a function of the same name. And the function becomes a task in leiningen. So for example to provision a machine all I have to do is:
; In file leiningen/aws_provision.clj
(ns leiningen.aws-provision)
(defn aws-provision [project & args]
(println "pallet code to provision the box here..."))
Then from the lein prompt I can do:
lein aws-provision
I guess pallet-lein isn't going to really be able to do what I want, because the details of which cloud provider, which machine size, which packages to install on the machine, etc. will be different for each person.

Executing code at regularly timed intervals in Clojure

What's the best way to make code run at regular intervals in Clojure ? I'm currently using java.util.concurrent.ScheduledExecutorService, but that's Java - is there a Clojure way of scheduling code to run at regular intervals, after a delay, cancellably ? All the Clojure code examples I've seen use Thread/sleep, which also seems too Java.
Well worth looking at the source code for Overtone, in particular the code for scheduling events at a particular time.
It's a music synthesis system so you have to hope they have the timing code right!!
Also they have helpfully separated the timing code out into a separate project (overtone/at-at) so that you can easily import it if you want. This provides a nice Clojure wrapper to the underlying Java libraries (i.e. ScheduledThreadPoolExecutor and friends). The syntax is like this:
;; run some-function every 500ms
(every 500 some-function)
You can also shedule events at specific times:
;; run some-other-function 10 seconds from now
(at (+ 10000 (now)) some-other-function)
From the clojure website http://clojure.org/concurrent_programming:
In all cases, Clojure does not replace the Java thread system, rather it works with it. Clojure functions are java.util.concurrent.Callable, therefore they work with the Executor framework etc.
It sounds like you are already doing it the right way.
(import 'java.util.concurrent.Executors)
(import 'java.util.concurrent.TimeUnit)
(.scheduleAtFixedRate (Executors/newScheduledThreadPool 1)
#(println "Hello") 0 5 TimeUnit/SECONDS)
I answered my own question # Implementing a cron type scheduler in clojure
maybe cronj might help?
The "tools.timer" library is a Java Timer wrapper: https://github.com/Ruiyun/tools.timer
It is very easy to use:
(use 'ruiyun.tools.timer)
(run-task! #(println "Say hello every 5 seconds.") :period 5000)