read-line not working in clojure REPL - clojure

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.

Related

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!

Forcing Cake to reload functions from my .clj files

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.

Is it possible to call a lein-plugin function from a lein-repl?

I want to control a number of lein-plugins (lein-cljs build, lein-aws, lein-beanstalk) from my lein repl. Is there a way to do this?
For example, I want to be able to call
plugin/src/leiningen/cljsbuild.clj: once
from the repl -- however, I apparently can't (require 'leiningen.cljsbuild) into my lein repl.
Thanks!
I'm not sure if this addresses what you are actually trying to do, but do you know about lein interactive mode? EDIT: Since lein interactive doesn't exist in the new version of lein, the solution may be in some use of jark. Jark has an interactive mode and a plugin to use lein, so it may be possible to issue both lein commands and call clojure functions from a single interactive prompt.
The Vinyasa library claims to provide this functionality. At the time of this writing, vinyasa.lein is broken, but it may be fixed in the near future.

"Hot swapping" code with swank clojure, and crash resilience

I've been messing around with developing a game in clojure, and one thing I've been really excited about is hot swapping in code.
I've been using swank clojure and emacs with the lein-swank plugin.
My main problem has been that of typos. Say I update a function, make a small error, and then hit Ctrl-C Ctrl-C to send it off to the REPL:
(if (> (rand) .5) (println "yay") (println "boo"))
(I should have written 0.5, not .5.)
In that case, the entire program will simply crash and burn, and I'll need to restart the whole thing. Hot swapping is great, but if I can't make even a tiny error then what's the point?
So what exactly is the workflow here? Am I missing something? Or is there a way to make swank clojure more resilient to these little errors? (I imagine that the best thing would simply be to reset to a previous working state, although that may be a little difficult.)
Thanks!
The program shouldn't “crash and burn”—it should raise an exception and throw you into the debugger, which you can dismiss by hitting Q (sldb-quit). After dismissing the debugger, the program is supposed to continue running normally. If this is not what happens, your SLIME configuration is probably broken somehow.
Personally I recommend C-M-x over C-c C-c. I don't think either one should have the problem you're experiencing, though, so switching may not fix it.

How to capture stdout/log output of an agent in SLIME?

I send a task to an agent, but can't see the output of any print statements I've put into the function. How do I trace code execution on agents?
EDIT: Turns out I do get the output in the console, but not in SLIME. The question now is, how to see the output in SLIME?
The key is to start swank from emacs as the inferior lisp process instead of calling lein swank from the shell. One way to do this is to use elein (the command is M-x elein-swank). Then you can either inspect the output in the inferior lisp buffer (which is called *elein-swank* in the example of using elein), or execute slime-redirect-inferior-output and have the output inline in the repl. Clojure.contrib.logging is a useful tool for sending log output.
You could wrap the calls in your repl in (with-out-str (def result (my-code))) to capture the output as a string. this way you can do it for just the code you're working on and you dont have to restart your repl to start getting your output.