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!
Related
In Clojure for Java Programmers Part 1 talk Rich Hickey mentions this as one of the benefits of Clojure:
If you build an application with some access to the ability to load
code - either a remote repl connection or some way to do that. Your
running production systems will have this capability to have fixes
loaded into running programs.
I'm wondering how easy it is in practice. Let's say I know there is a bug in one function and I want to redefine/override it in production via remote repl
Is it possible?
Will the overridden version stay active after I exit the repl?
Is this behavior in all JVM application containers?
I don't have any experience of using JVM in production and that's the reason for asking this question.
Is it possible?
Yes, I would recommend watching The Joys and Perils of Interactive Development - Stuart Sierra he tells a story of NASA hotfixing a satellite in space using the REPL.
Will the overridden version stay active after I exit the repl?
When you reevaluate the function the changes will apply everywhere so for example
(defn add1 [x]
(+ x 2))
(defn foo [x]
(add1 x))
(foo 1)
Notice (foo 1) => 3 if you fix and reevaluate add1 then run again (foo 1) => 2
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.
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.
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.
Whenever I call a clojure function that gets a users' console input using (read-line) through SLIME or a normal REPL, the function returns immediately. I've resorted to jar'ing up my project and running it to test but this obviously isn't quick or sustainable.
Is there some trick to getting console interaction through a REPL working or is it not possible? If not are there any good workarounds?
This will work now with swank-clojure 1.4.0-SNAPSHOT if you wrap the call to read-line in swank.core/with-read-line-support like this
(with-read-line-support (println "a line from Emacs:" (read-line))
https://github.com/technomancy/swank-clojure/commit/f4a1eebc4d34f2ff473c4e5350f889ec356f5168
Currently there doesn't seem to be any way of reading console input through swank-clojure; calling (read-line) simply returns nil in 1.4.0-SNAPSHOT and hangs in earlier versions. Installing 1.4.0 as a user-level leiningen plugin seems to be the best bet right now. At least the REPL will return to your control at some point.