Lighttable and Quil - clojure

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.

Related

Using Clojurescript with Light Table

I have been trying to use Clojure Script with Light Table and lein-cljs without much success.
The main problem seems to be with dependencies. It seems like evaluating a cljs (ctrl-shift-enter) page does not recursively evaluate the dependencies. The result of this is that I have to manually evaluate each dependent file. If I refresh the HTML page, or make a change I have to do this all over again. Other times evaluating a page doesn't work, and just gives me good not found error.
Clearly this is not usable. Am I doing something wrong? For Clojure it seems to work just fine.
Stack Overflow is probably a bad place to learn more about Light Table. I'd drop into the Light Table channel at #lighttable on irc.freenode.net. Somebody can help you there!

LightTable IDE legacy REPL mode?

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.

Can you save your Clojure REPL's state (or, effectivelly, can you program complex programs using REPL?)

After defining variables, functions, etc., can you save what you have done on the REPL too an text .clj file?
most people work with the repl through an editor such ad Eclipse/Emacs/vim and that editor has the ability to save the repl, though without some diligence on the developers part this will likely be an incomplete record of what happened. Some of the state of the repl may have come from loading files etc which will be in a different state.
So the short answer is typically not.
In Linux (mine = Ubuntu 16.04.2 LTS) if you are using lein then check for .lein (hidden directory) and look for repl-history. You should find the commands that you have typed or pasted into the REPL. This can be a source for later edit - I use geany...
I am answering the parenthetical part of your question. For me, the Clojure REPL is very useful for testing functions and proving out concepts that take no more than a few lines. I will often put hooks in a module that is not the main, just so I can load a file and run it through a couple of functions. I can also do this from main using the same mindset; that is write a debug function.
I found the Eclipse plugin to be quite useful, but I do not use it much these days, mostly Vim and running the module with one or more special functions and running the main. I don't know of any way to save REPL state.

C++ Keywords not Colored in Emacs

I have been using emacs for a while for mainly python programming, and have started C++ coding with it. When I open a c++ file, it opens without problems with c++-mode. The background and foreground colors are normal for the theme I have with color-theme, but keywords and strings are not colored differently. Below is the code in my .emacs to initialize color-theme.
(add-to-list 'load-path "D:\\emacs\\color-theme-6.6.0")
(require 'color-theme)
(color-theme-initialize)
(setq color-theme-is-global t)
(color-theme-hober)
I have not put in any code for c++. Any ideas?
Edit: I tried turning off color-theme to see if at least then there would be some coloring, and there was not, even after ensuring font-lock-mode was on. Also, this is GNU Emacs 23.1.1
Place the cursor over a keyword and do M-x describe-face. The face should be identified as font-lock-keyword-face and the description of the face given. At that point it should be easier to determine whether the keyword has the wrong face, or the face just has a default appearance. You could also double-check that font-lock-mode is on with describe-mode.
I finally got syntax highlighting by removing a folder called site-lisp from the folder d:/emacs. My Emacs installation is in d:/emacs/emacs-23.1, and somehow it was reading from these files. So, removing this folder forced Emacs to use the correct ones, I think.
(font-lock-mode) should help (with non negative argument to make sure it's turned on)
Generally this should work straight out of the box on a standard install - to verify start emacs with the "-q" option to avoid loading stuff from your init file. I have verified this with a quick install (on XP Pro) of the official 23.2 binaries. Here's the output: (apparently can't post images due to low reputation - the link is img444.imageshack.us/img444/2680/46117077.png).
If this displays code as you expect, then there's an issue with your init file - I suggest using the standard approach of commenting everything out and selectively adding things back in, until you come across the offending line(s).

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