LightTable IDE legacy REPL mode? - clojure

I found
Light Table
is a great tool to try Clojure language.
It has a handy feature called Instarepl -
;; Anything you type in here will be executed immediately with the results shown on the right.
However, I can't find a way to use a legacy REPL mode which I can run a small code such as
(take 100 (iterate inc 1))
Typing the code, sure the Instarepl immediately eval and show
(1 2 3 4 5 ...)
, but does not exactly shows the result.
Please guide. Thanks.

Light Table uses leining for the project management, so you can very easily keep repl open in another window connected to the same project to get the traditional REPL expierence. This lets you switch between the two quickly. Light Table is evolving rapidly and who knows if standalone repl mode has been added since I last looked or will be added soon.

Well, on a second try, I found out that clicking the animated-blue-block-icon shows a bottom pane (console), into which for example println outputs. This was what I needed.

Related

Cider debug -- how to evaluate stuff while debugging

Cider debug instructions tell me I can press e to evaluate something while debugging. This gives me a little one-line space in Emacs mini-buffer at the bottom.
Is there a way to switch to the full REPL while in the middle of
debugging a function, with access to all the locals, etc.? Currently
the REPL is hung/frozen while debugging. I'm thinking of something in
the style of how PyCharm or Matlab allow full REPL while in the middle of something.
It does appear that the jacked-in REPL is tied up during debugging.
But there are a few options available through the debugger that may
give you nearly as much as you'd get out of the REPL. A handy one is
to inject a new value for the result you're about to produce.
So you're actually changing the data on-the-fly.
You can inspect the full list of local vars with l. Then see more
about a var with inspect and specifying which.
You can also eval to enter an arbitrary expression just like
you would in the REPL (as you've mentioned). That seems to be a
single-line full REPL, with history, editing, etc. Is there something
you'd want to do in the REPL that you can't do with e or discover
with l or p?
One thing I find really frustrating is that I can't edit a function while the debugger is stoped at the said function, then edit it and re run it with the initial arguments. In cider, if you try to edit a function being debugged, emacs will open the bebugger in a new buffer with the original code. Alternatively, you have the e command that evals things in the minibuffer, which I don't think is a great experience. The closest I came to this is the following:
Imagine you have some function that crashes and you need to debug:
(defn some-fn
[complex-data more-data]
; block of code with some bug
)
I'll create atoms in the namespace and set the value inside the given function:
(def c (atom nil))
(def d (atom nil))
(defn some-fn
[complex-data more-data]
(reset! c complex-data)
(reset! d more-data)
; block of code with some bug
)
Then I'l just iterate on some-fn using the args I now have available in the namespace.
(some-fn #c #d)
I think it's a much better approach than using the eval command and the minibuffer from the cider debugger.

Lighttable and Quil

I apologize that I'm new to Clojure. I've found there's not much in the way of tooling or tutorials for the Clojure world that I've been able to digest so far, so I've downloaded Light Table ( http://www.lighttable.com/ ) which sounds like it should make it easier. I'm on Windows 7 64-bit and the basic Light Table UI seems to be working.
To try to experiment further with Light Table / Clojure, I've also downloaded Clojure drawing library Quil ( https://github.com/quil/quil ), but after instructing Light Table to connect to \quil-master\project.clj and then opening and executing various files from \quil-master\examples (such as automata.clj or graphics.clj), Light Table produces what I assume is a spinning blue "thinking" icon that gets stuck or eventually ends with "nil" or "java.lang.Math".
Only "example2.clj" seems to work (although not always) in that it (usually) pops up a window with a color-changing diamond. What do I need to do to make these other Quil examples work in Light Table?
(On a side note, I've also downloaded Mire (another Clojure project on Github), but it also won't run at all with Light Table.)
This answer was for LightTable 0.2.0. I have not yet tried the new 0.3.0.
It makes perfect sense to try and pick LightTable and Quil. I think the pain is mostly due to a chicken and egg problem.
LightTable assumes some clojure knowledge (at least as regards project management) and doesn't provide a "standard" repl, but rather is a test-bed for an even more interactive style of working.
Quil assumes comfort in a standard clojure repl environment and is wrapping a lot of complexity of an underlying java project (Processing).
Also the whole purpose of Quil is to generate "side-effects" (whole other graphical displays) and LightTable is trying to help you work with functions and their return values, not the side-effects of functions.
If you are connecting to quil as a project, opening example2.clj and executing it (Ctrl Shift Enter) and getting a light blue "nil" - Congratulations you have a working system!
(At least I can duplicate the exact same behavior and the example seems to work).
The first time you do this it may take a while for LightTable to download all of Quil's dependencies (maybe why LightTable appeared to hang - or maybe Light Table and Quil have an interaction bug I have yet to trigger.)
That "nil" is the return value resulting from Light Table executing the file (in the context of the running project) (see footnote). Somewhere (perhaps hidden behind a full screen Light Table) a new window should be created with the example running.
The "java.lang.Math" value of executing automata.clj is also consistent with what I see.
Here the trick is to generate a call to defsketch which example2.clj did inline but automata.clj does not.
You can create the sketch by calling the function run-rule:
After executing the file automata.clj (to get all the function definitions loaded) add the following line to the end of the file in the editor:
(run-rule 101 {:width 100 :height 100 :scale 4})
Put the cursor after the closing paren and execute the single form with Ctrl-Enter. Again a new window (possibly hidden behind some other window - try task switching with Alt-Tab) should open running the automata demo.
With Light Table connected to the quil project and automata.clj already evaluated you can also get the same effect by opening an "instarepl" and typing:
(clj-automata.core/run-rule 101
{:width 100 :height 100
:scale 4})
Try editing the values and seeing what happens. (Again the whole nature of Quil being side-effecting makes this not quite so elegant in LightTable but it seems to work.)
Footnote:
I am not very familiar with LightTable. I would have expected the light blue to be the result of evaluating the last form in the file but this seems to not to be the case. Using Cntl-Enter on the last line reveals "#'example2/example2" not "nil".
I am also not very familiar with Quil and defsketch is macro not a regular function so maybe that is interacting in an odd way with LightTable.

How can you dump contents of Clojure REPL to a file?

So I have been working on a Clojure tutorial and it's pretty fun so far. Unfortunately, every time I close my REPL out, I lose all of the defn and def that I created in the previous session.
So, to save time in the future, is it possible to have the Clojure REPL save everything I've typed to a file so I can dig out what I need for future uses?
I think most people work by using their IDE to send fragments of code to the REPL, rather than doing a lot of direct hacking in the REPL. This way you have a better idea of the current state of the program.
If you use Emacs and SLIME, you can save the REPL buffer to a file just like saving any other buffer.
If you are using a Unix-like operating system you can start your REPL using rlwrap. This would give you Ctrl-R for accessing commands from history in an easy way, as well as storing the history itself.
You'd just have to install rlwrap and prepend it to the line where you start REPL, e.g. rlwrap lein repl
Couple of tips:
I do a lot of coding in the REPL, but as soon as I write something that works then I copy/paste it into a source file. Then I can just run or reload that source file whenever I want to get a REPL environment back to a fully configured state.
Some REPLs (e.g. the Eclipse Counterclockwise REPL that I use) keep history from previous sessions so you can up-arrow to the earlier REPL commands you want to repeat.
This is actually a harder problem than it seems because a dump like this must take into account symbol and vars dependencies and output to file in the correct order of symbols. That said since code is data one should be able to write a function that does that.

Code completion in CounterClockWise?

I am a first-time user of CounterClockWise, the Eclipse plugin for Clojure, and it appears that code completion just won't work. I keep wondering whether the problem lies between my keyboard and my chair, but can't find a solution. I just installed CCW on Galileo build 20090920-1017 and MacOSX. Then I try to write some lines of clojure code, and no code completion widget comes up. I'm using the Java perspective. Am I missing something? I somehow hoped it would work out of the box, as I'm just starting with Clojure.
Many thanks in advance!
Costas
I'm using CCW 0.0.62.STABLE01 and code completion works. Press control space to bring up a list of possible items and as you type, the list is filtered. Press return at any point to accept the first item in the list or use the up/down arrows to select another item. Press esc to dismiss the list.
I found the answer here (http://www.chaomancy.com/blog/clojure-in-eclipse-part-2-counterclockwise-maven.html):
"But most of Counterclockwise's features come to life once you start a REPL. For Counterclockwise is really built around the REPL; without one running, things such as code completion will not work."
I dont see code completion in the ccw list of completed features though I dont know how well this feature list is kept up to date
edit: its now marked completed.

Easy ways to try out and test Lisp syntax?

Clojure has introduced me to the concept of Lisp syntax, and I'm interested, but it's a pain to get the Clojure repl set up and use it on different machines. What other resources are there out there for actually on-the-fly testing and playing with Lisp syntax?
I'm imagining something like a website where you can input rudimentary code, or a browser add-on, or even just a standalone application that steps you through Lisp (something that runs on Linux/Ubuntu).
Haven't been able to find anything like that to start me off in a simple/accessible way.
The SISC Online REPL is exactly what you need. It accepts Scheme syntax, which is a variant of Lisp.
For a standalone app I like PLT Scheme because it seems to work the same on every platform I've tried. I was previously using MIT Scheme on Ubuntu, but decided to switch when I bought a new machine with 64-bit Vista installed.
At the risk of losing all of my rep points, try newlisp.
I've used LispBox in the past. Easy to set up and get going, but I have to admit that Emacs is second nature to me. If you're unfamiliar with that, you may can use another editor, but getting intimate with Emacs will help you live comfortably in Lispville.
It's not hard to get to a Clojure REPL nowadays.
Go to http://build.clojure.org/ and download clojure.jar.
Install a JVM.
java -jar clojure.jar
This should work on any machine that has a JVM installed.
For more fun, install rlwrap and use rlwrap java -jar clojure.
For learning Lisp the LispWorks personal edition is fine: LispWorks downloads. There are versions for Mac OS X, Windows, Linux and FreeBSD. All have the same user interface capabilities and this includes an editor and listener - plus lots of other tools. On Linux and FreeBSD in currently (LispWorks 5) uses X11/Motif for the GUI - this will change in a future version (LispWorks 6) to GTK. So to run it now under Linux, you need to have the Motif lib installed. Other than that using it is relatively painless. There are some restrictions to it (only 5 hours runtime, then you need to save and restart, ...) and the full version is commercial. But for learning some Lisp basics, it is quite good.
Franz has an online REPL for Allegro Common Lisp.
http://www.franz.com/products/allegrocl/prompt/
codepad.org is a nice simple online way to enter and run code (in many different languages, including Scheme). I use it all the time for testing out snippets of code to use in SO answers.
I won't edit Lisp code without an editor that does what Emacs will do with the following configuration:
(define-key lisp-mode-shared-map "[" 'insert-parentheses)
(define-key lisp-mode-shared-map "]" 'move-past-close-and-reindent)
The former inserts balanced parentheses. If you give it a numeric prefix argument, it inserts that many nested pairs. By default, it inserts one. If you have a text region marked, it encloses that region in the innermost inserted pair. That means that you'll never open a parenthesis that's not closed.
The latter is harder to explain, as it's used less frequently. It's more of a navigation command than an insertion command. It confirms that you're done editing the current form and moves the cursor up, out, and past it, preparing for the next likely insertion.
With these keys bound, one no longer needs to use the Shift key to access the parentheses. Also, this leaves the parentheses keys bound as normal, for when sexp repair or a literal parenthesis character is required. I stole the bracket keys because they're used so infrequently in Emacs Lisp and Common Lisp. The bracket characters are still accessible with meta key bindings:
(defmacro make-key-inserter (def)
"Substitute for `self-insert-command'."
`(lambda (arg)
(interactive "*P")
(insert-char ,def (prefix-numeric-value arg))))
(define-key lisp-mode-shared-map "\M-[" (make-key-inserter ?\[))
(define-key lisp-mode-shared-map "\M-]" (make-key-inserter ?\]))
It's not essential to use Emacs, but don't settle for less with another editor that can't at least match this capability. There's also a whole family of commands for navigating and manipulating the sexp tree as a tree. Understanding why that's valuable will require you to fumble around for a while until you stop seeing syntax and start seeing the tree.
Since you're using Ubuntu, I'd just install some of the packages and give them a try.
Common Lisp: apt-get install clisp
Scheme: apt-get install drscheme
These aren't the only packaged implementations, but maybe the easier to get started with. You'll be able to play at a REPL and also run your own scripts through the interpreter.
BioBike provides a full Common Lisp evaluator through the web (with extras like a knowledge representation system, biocomputing, and an alternative visual langauge as well).
If you just want to play with LISP, interactively, quickly, GNU Emacs has a LISP interpreter built in, and listening in the *scratch* buffer. Type an S-expression, position immediately after it, and then hit Ctrl-J to evaluate it. Or <ESC>: will put an Eval: prompt in the minibuffer, accept an S-expression, and evaluate it.
You can define functions, using defun, and test things that way. You have access to ALL of the GNU Emacs built-in functions, and anything you have loaded. If you want to keep things around, you can put them in your .emacs file.
https://github.com/fogus/himera
was very easy for me to setup on a https://www.nitrous.io virtual box. Makes a nice web interface.
Downloading and running clojure-1.6.0.jar on my windows (XP) box was as easy as using this script to run the REPL :
java -cp clojure-1.6.0.jar clojure.main