sp5repl
A Scala Read-Eval-Processing Loop
sp5repl is a small layer around Scala and Processing that allows you to write Processing sketches dynamically using an interactive read-eval-loop. The code entered into the Scala REPL is actually compiled, thus it runs at full speed; we get most of the flexibility of Jython and Clojure/Quil with the speed and error checking of Scala. It has been tested on Mac OSX and Ubuntu.
Getting Started
web: http://drablab.org/sp5repl/
bitbucket: https://bitbucket.org/keithohara/sp5repl/
download: https://bitbucket.org/keithohara/sp5repl/downloads/sp5repl.tgz
Currently three tools are provided:
sp5repl
: A Processing read-eval-loop with a default sketchsp5scalac
: A Processing aware scala compilersp5scala
: A Processing aware scala interpreter
Some examples:
>./sp5repl
sp5repl>size(600, 600)
sp5repl>background(24)
sp5repl>smooth()
sp5repl>fill(196, 128, 64)
sp5repl>ellipse(width/2, height/2, 150, 150)
sp5repl>fill(64, 128, 196)
sp5repl>for (i <- width to 0 by -1) ellipse(random(i), i, i/20, i/20)
The above example shows how to create a sketch line by line. It creates an 600 by 600 window with a dark gray background. Smooth drawing is enabled and orange is used as the fill color. One large ellipse is drawn in the center of the screen along with many randomly placed blue circles.
>./sp5repl examples/draw.sp5
sp5repl>stroke(255, 0, 0)
This code runs one of the provided drawing examples and changes the line color dynamically while the sketch is running.
>./sp5repl examples/draw.sp5
sp5repl>noStroke()
sp5repl>(64, 128, 196)
sp5repl>def drawEllipse() = ellipse(mouseX, mouseY, 5, 5)
sp5repl>onMouseDragged(drawEllipse)
Like the previous example, this code runs one of the provided drawing programs and changes the line color dynamically. In addition, the drawing method is changed to draw ellipses while the sketch is running.
The Good
- Interactively construct a Processing sketch
- Manipulate a Processing sketch at run-time
- Create multiple sketch windows using
createSketch()
- The Scala REPL supports a command history and tab-completion (allowing quick look up of processing methods)
- The code entered in the REPL is compiled and is fast.
- Automatically grabs Processing libraries from ~/Documents/Processing/libraries
- tested with fullscreen and gsvideo
sp5repl can be used with Emacs or Sublime Text via normal scala modes assuming it's in your
PATH
emacs:
(setq auto-mode-alist (append '(("\\.sp5$" . scala-mode)) auto-mode-alist)) (setq scala-interpreter "sp5repl")
sublime text via SublimeREPL package. Add this to the SublimeREPL/config/Scala/Main.sublime-menu:
{"command": "repl_open", "caption": "sp5repl", "id": "repl_sp5repl", "mnemonic": "p", "args": { "type": "subprocess", "encoding": "utf8", "external_id": "scala", "cmd": {"linux": ["sp5repl", "-t"], "osx": ["sp5repl", "-t"], "windows": ["sp5repl", "-t"]}, "soft_quit": "\nexit\n", "cwd": "$file_path", "cmd_postfix": "\n", "extend_env": {"osx": {"EMACS": "1", "PATH": "{PATH}:/usr/local/bin"}, "linux": {"EMACS": "1", "PATH": "{PATH}:/usr/local/bin"}, "windows": {"EMACS": "1"}}, "suppress_echo": false, "syntax": "Packages/Scala/Scala.tmLanguage" } }
The Bad
- In order to compile a
.sp5
file usingsp5scalac
, a slighly more verbose syntax is necessary. see:examples/compiled/draw.sp5
Maybe Scala's new macro system can help.
The Ugly
- The REPL doesn't clean up garbage in some cases causing the the interpreter to slow down or making a restart necessary. see:
https://issues.scala-lang.org/browse/SI-4331
- No .bat file for running sp5repl.sp5repl on Windows
Tips
:load filename.sp5
inside the REPL will load a file.exit
leaves the REPL.newSketch()
- will activate a new processing sketch and make its methods visible at the top-level.createSketch()
- create and return a new processing sketch, but does not make its methods visible at the top level.sp5repl.sp5repl
exists as a single source file that can be compiled and used independently of the provided shell scripts. Just make sure the Scala and Processing jars are in your classpath.
Motivation
I teach two introductory computing classes: one using Python (IPRE's Calico) and robots and the other with Processing. This project was an attempt to build a tool that leverages the best of both of these worlds.
Python's interactive read-eval-print loop (REPL) is helpful for students building an intuition for programming. More generally, the REPL is useful for quick calculations, debuggin, prototyping, and testing. Python's easy-to-use lists, dictionaries, and libraries make many tasks simple enough for CS-1, e.g., slurping in files and doing text analysis. Python's dynamic typing and syntax make programs succinct and there is little boilerplate code.
On the other hand, Python can be quite slow, making things like image processing in Python problematic. Image processing tasks like convolution are no problem in Processing because it compiles to Java. Along with the large collection of libraries, and the great community around Processing, it is a pretty good choice for introducing computing. In my experience, students were able to write really interesting programs in one semester. Sadly it is still Java -- which in my experience teaching introductory students, has lots of baggage compared with Python. Java's obtuse syntax due to its type system, lack of operator overloading, and semicolons confuse introductory students.
Personally, so far anyway, I have found Scala to be a nice statically typed replacement for Python. The curly brace delimited blocks aren't as common since everything is an expression. The type inference removes lots of redundancy. And of course, the higher-order functions and for comprehensions make programs pretty. I hope to try it out in an introductory course next year.
Keith O'Hara
Jan 2013
sp5repl>