I'm doing my first steps with Clojure, but for some reason the up/down keys don't allow me to step through the command history in the REPL like in REPLs of other languages. Does the Clojure REPL use different keys to access the command history, or is this feature just not (yet) implemented?
If you are, by any chance, using a Unix-like environment you can use rlwrap to to achieve nice things like command line history and Ctrl-R for searching through it.
Just prepend your command which you use to start repl, e.g:
rlwrap java -cp clojure.jar clojure.main
Or use Leiningen or Cake. Either of these are useful in general, and you'll be happy to learn to use them, but specifically they use readline or jline automatically where available.
You can enhance the REPL to do this by using JLine, see here.
Depends which REPL you use. Most good REPLs have this functionality and more.
If you're coming from Java as an Eclipse user, for example, you may want to use the embedded nREPL provided by the Counterclockwise plugin. I found this easier to use than configuring a separate dedicated Clojure environment.
C-, M-x slime-repl-forward-input
C-, M-x slime-repl-backward-input
Go to the next/previous history item.
http://common-lisp.net/project/slime/doc/html/Input-Navigation.html
Related
I am new to Clojure, and I am usually using it from (Spac)emacs, where the repl formats fine. However, when I try to use the repl within a gnome-terminal, then the formatting is broken, i.e. after 'sending' some code, the line of code gets 'reformatted' as shown in the screencast below:
Anybody has some idea what is going on here? The terminal is just a basic 'gnome-terminal' on Fedora.
B.t.w. the same screencast is used for reporting an issue with criterium about the bench example hanging.
Disclaimer: this is not a solution to the problem, but it shows,
where the problem lies and what are workarounds.
TL;DR: call clojure (which does not use rlwrap¹; use
Rebel-Readline
instead, for more features
clj calls clojure, but checks, if rlwrap is installed and uses
that to call clojure with some settings, that are suitable for a Lisp.
rlwrap is a great tool, to get readline capabilities with
interactive CLI tools, that don't have it (e.g. emacs/vi-mode, history,
"hippy" completion). But in this case it is the culprit to smash up the
REPL. What's underlying problem is not clear, but in cases like this
the options usually are: buggy software (the terminal, rlwrap, or the
way Clojure interacts with the terminal), wrong/buggy TERM settings or
term-capabilities.
That said, rlwrap might be the "just good enough" option here
anyway. First of all Clojure developers tend to use the REPL via the
editor anyway. Second there is a far superior option to get what
rlwrap brings to the table:
Rebel-Readline
Beside emacs/vi-modes and history it brings (and probably more):
syntax highlighting for both input and output (with pretty-printing)
multi-line edit in the REPL
proper auto-completion
showing the doc-string of the function under the cursor
¹) From: https://github.com/hanslub42/rlwrap
rlwrap is a 'readline wrapper', a small utility that uses the GNU Readline library to allow the editing of keyboard input for any command.
This is a rlwrap bug that has been fixed upstream (but, as of now, is still open in several distributions) . In a nutshell, either:
use a recent rlwrap (0.46-1 or newer),
or avoid the very newest readline (8.2 or newer),
or put set enable-bracketed-paste off in your inputrc
See this bug report on Github (or this summary of the history of this issue)
Hans Lub (rlwrap maintainer)
I'm having trouble getting started with ASDF and Common Lisp.
How can I get a minimal example working?
To answer my own question:
So, how to do your "Hello World!" with ASDF build system? Here's what worked for me. I'll help you side-step the time-consuming traps that I fell into.
Watch this video. He explains installation of sbcl, emacs, quicklisp (a Lisp library manager), slime, and quickproject, which sets up a project with ASDF.
Now to side-step a few traps:
slime didn't seem to work with clisp. I had to use scbl. Note, I'm on ubuntu 16.04.
You must have your .asd file in the path of your ASDF system. You can accomplish this by working in ~/common-lisp/; if you don't have it you can make it. See here to setup ASDF to look in other directories.
Once you have they system built, load the system with (asdf:load-system :<system>). Also, to access functions in a different package, use the following syntax: (<package>::<function> ...).
Happy coding.
For vim users: Emacs and Lisp, specifically with quicklisp and emacs's package manager, is a beautiful combination. Probably because Emacs is written in Lisp. I see the value of this. But I'm a vim user. I was pleased to find Evil, a great vim layer for emacs. Check out this 4 minute video, which impressed me. Also, here is a post of another vim user setting it up to be like his vim environment, better in some cases. I now think the combination of emacs and vim is better than either alternative.
I have been learning Clojure a bit recently. Is there such a thing in Clojure world as Scala-like worksheets, into which I can put any code and get it evaluated as soon as I save it? Or maybe there's a similar solution for Clojure?
I am now working with lein repl and, sadly, can't say it's the most usable tool ever.
In Lisp development in general (and Clojure in particular) the preferred programming style is what's usually dubbed interactive programming - the developer keeps an image of the app loaded at all times and interacts with it via a REPL. You can easily modify the loaded code on the fly and test changes immediately in the REPL (that's not easy at all with Scala - one has to resort to something like JRebel to do it). I find the Scala worksheets a pretty primitive solution in comparison...
The workflow that I follow in Clojure is:
I open nREPL.el in Emacs - this loads my lein2 project with all of its dependencies and gives me a REPL which I can use the try out stuff
I write some code in source code and load the changed functions (maybe by evaluating a top level form with C-M-x
Afterwards I'd press C-x C-z to jump back to the REPL and I try out the new code in it
I go back to step 2
Basically the Clojure REPL is much more powerful than the Scala REPL and I personally consider it hugely superior to the Scala IDE worksheets. Once you get used to the interactive incremental style of programming Lisp offers everything else starts to look strangely complex by comparison. I'm not familiar with Eclipse's CounterClockWise Clojure plugin, but I'm pretty sure it offers similar functionality to Emacs's nREPL.el.
You might want to take a look at the autoexpect plugin for Leiningen. Every time you save any file in the working directory, the plugin compiles and runs your code; as a bonus, it will evaluate any "expect" function calls which can serve as tests. This is very helpful for test driven development and is a nice compliment to working with the REPL as described in the other answer (I often use one or the other or both together depending on how many test cases I have in place).
I should note that running autoexpect is far faster than running "lein test" or "lein run" repeatedly, due to the startup cost of the JVM and Leiningen.
It sounds like what you are looking for is the Clojure Koan plugin. This a worksheet-style problem-solving exercise tool that watches your edits and provides instant feedback on the correctness of your work.
For actual development workflow I second the advice others here have provided on tooling and interactive environment setup, but since you specifically said you are learning Clojure, this can be a fun approach. There is also a web application called 4Clojure that you might have fun playing with.
However you will eventually (or right away) want to get a smooth and convenient development environment set up, and I haven't seen any mention so far of a few important tools. One person mentioned Nrepl. If you like Emacs, there's a slime/swank-like interaction mode that jacks into nrepl called nrepl.el that provides very nice integration between editing files and messing around in the repl. Similarly there is VimClojure, and you can find plugins for IntelliJ (LaClojure) and Eclipse (Counterclockwise) that are also popular and well-maintained.
Someone mentioned autoexpect. If you are looking for a good TDD setup, I would recommend midje. If you are using a 2.0 preview release of leiningen there are a few issues with the lazytest integration being in flux, and lazytest itself is or should be deprecated. But I prefer midje over expectations, personally, and these problems will surely be worked out in the 2.0 release of lein-midje. The stable version of lein-midje that works with the non-preview (1.x) leiningen has autotest-like functionality using lazytest.
As nobody has mentioned it, cursive is really nice these days, although I was sad to move back to a full blown IDE. You can easily do the same thing as Emacs with the built in repl, just move into the namespace that you are working with and synchronise every time you make changes. It's a really nice way of working.
In my clojurescript code I have the following:
(defn onload [] (repl/connect "http://localhost:9000/repl"))
(set! (.-onload js/window) onload)
The Clojurescript repl is very useful in development, but I am hesitant to leave it in the code during production. What is the cleanest way to have the above code present during development (simple compilation), but absent during production (advanced compilation)?
The modern-cljs tutorial actually describes exactly how to solve this here.
Hope that helps!
Unfortunately there aren't currently any well defined ways to do conditional compilation in ClojureScript.
You could add configuration variables to control whether to start a REPL in a variety of ways, but one quick and easy way would be to get the hostname of the current page, and only invoke repl/connnect if it was "localhost" or whatever other domains you're using for development work.
I think a combination of lein2 profiles and cljsbuild src-paths munging can do the trick. eg, create a namespace that simply loads your repl, and exclude it with a profile run for the final compile (possibly might need to create a dummy namespace in another src-path directory).
I occasionally get this problem, and generally work around it, but it's rather frustrating.
I have all of Incanter (check it out if you don't know it: it's superb) on my classpath. I try to import it (through a Slime REPL) like this: user> (use 'incanter.core), but fail.
Doing this: user> (use 'clojure.contrib.def) works just fine, and this file is in the same place–on my classpath.
Regardless, the error isn't anything about classpath: it's this:
Don't know how to create ISeq from: clojure.lang.Symbol
[Thrown class java.lang.IllegalArgumentException]
You can see my entire terminal here (a screenshot.)
I don't know what's going on here, and it's really frustrating, as I really would like to use Incancter, and I can from the Incanter binary's REPL. I definitely don't want to develop from that–and this should work.
Any help would be much appreciated.
EDIT:
It appears as though Incanter requires Clojure 1.2, and lein swank gives me Clojure 1.1. This might be the cause of my problems: if so, is there a way to continue to use Swank & Lein with Clojure 1.2?
Thanks again!
EDIT:
Apparently if you start using Clojure-1.1 and lein swank, you're stuck with it unless you make a new project.
If future people have this problem, this article helped me out, but also, at least for me, you must start a new lein project if you had begun it using leink swank and Clojure-1.1. Simply changing your project.clj file and then lein swanking again doesn't work.
Yes, you can use Leiningen and swank-clojure with Clojure 1.2. You might need to use a recent version of Leiningen (I'm not sure if a certain old limitation affected lein repl only or was it lein swank as well; anyway, try the 1.2-RC2 which you'll find in the downloads section on GitHub). You will also need to use a recent-enough swank-clojure; I use a bleeding edge checkout myself, get yours here.
Other than that, simply use 1.2 jars for Clojure and contrib. (Lein uses it's own Clojure, separate from the one used for lein swank, for its internal workings and you never need to care about it; swank-clojure has no AOT'd namespaces and doesn't particularly care about the Clojure version, except once in a (long!) while something breaks, a patch is applied and joy is restored.)
I hope the above helps, but if it doesn't: your problem description is not entirely sufficient for me to get a clear picture of what is happening. Could you add information on what it means for "all of Incanter" to be on your classpath (do you mean the jars? sources? where do you get them? how do you set your classpath?). Without knowing this, it'll be hard to replicate your setup to try to track down the source of the problem.
Of course if bumping some versions fixes things, please disregard my current confusion. ;-)