Tuesday, July 14, 2009

Flowers and Spirals and iterated complex functions

Take a look at spiral.fnargs.com - it's a plotter for the function that generates the Mandelbrot fractal:

z <- z2 + c

These are some of the images it generates:

It's written in arc, using rainbow (in order to access java's image libraries; it could also be done using shell calls to ImageMagick from arc3).


Tuesday, May 26, 2009

Arc at XP Day France

Today's arc presentation at XP Day France -

Wednesday, May 20, 2009

Introducing Welder

When I read Gödel, Escher, Bach, I decided to drop my literature degree and take a computing one instead to learn Lisp. That didn't happen ... I got as far as expertly and elegantly configuring a key binding or two in my ~/.emacs file while struggling with java-mode. Many years later, arc came out, and Paul Graham being who he is, I just had to take a look. Maybe I could build an online ecommerce solution with this stuff, get rich, and turn into an Angel.

Anyway, the first thing I noticed was that I didn't have an editor for arc. Intellij wasn't going to cut the mustard, I wasn't falling in love with Textmate despite those gorgeous RoR screencasts, and I didn't feel like learning DrScheme. And none of these options would give me arc-aware highlighting and evaluation in any case.

So I decided to try what anyone learning a new language should try: write an editor for it. In it. On top of that, I needed something to drive rainbow that would demonstrate easy java integration without polluting the elegance of the arc spirit. And lo, Welder was born. Welder is an arc editor, written in arc. You'll have to forgive, or at least tolerate, the pun. The name "arc" invites it, what else could I do?

Don't get excited - Welder isn't quite ready for prime-time. It's too slow to parse a file (around 5 seconds for arc.arc - 2.5k lines, 13k tokens). The slowness is in the tokeniser, so I'm concentrating my speed-up efforts there. For smaller files (~3k tokens), its performance is tolerable.

These were the minimal required features I started with:

  • Syntax colouring: strings, numbers, characters, should have their own colours, and symbols should be coloured differently depending on their current bound value, if any. This way it's easy to see if a local variable is shadowing a globally bound symbol, and what kind of a thing it's shadowing.

  • Paren-matching - highlight matching parentheses, brackets, string delimiters, and any other kind of delimiter that might arise in the future. Also, highlight unbalanced delimiters.

  • Easy selection of forms: I'm addicted to intellij's "widen selection" - I want to be able to select the entire form enclosing the current expression, in such a way that repeated application selects an increasingly wider scope. This makes copy/paste operations a lot simpler.

  • Eval: I want to eval the currently selected expression, or the whole file, with a single keystroke

  • Htmlify: there's no point in writing arc if you're not going to blog about it. But what a pain to reproduce all that syntax highlighting! and besides, if arc gets its herokugarden, this might be a useful part of it.

  • Configurable keystrokes for all actions

  • Pretty-printing (doesn't work terribly well)

  • As well as all the usual editor facilities: undo/redo, cut/copy/paste. Fortunately, java/swing comes with this kind of thing pre-packaged.

Added later:

  • Font-size control - essential to demo welder on a projector

  • "pop-form" - replace (foo (bar)) with (bar) if the cursor is on bar

  • "push-form" - replace x with ( x) - these two together are really useful for inserting/removing a little do, atomic, time, debug or whatever you need for quick refactorings

Note the paren-matching:

And easy form-selection:

Context help for symbol under caret (help text, if present, and the help system, are entirely thanks to the tireless efforts of anarki contributors)

And the complete list of current keybindings:

Welder is still outrageously incomplete. Here are the kinds of thing I would like to add:

  • Visual macro expander

  • Common refactorings that are annoying to do by hand - toggle between let and with for example

  • Navigate to source of symbol declarations

  • Appropriate highlighting for lexically-bound symbols (I don't know how to do this when the scope is created by a macro, other than macexing everything and working backwards)

  • Runtime analysis

  • and everything else ...

Please suggest and vote for your favourite features!

Welder requires anarki, and is distributed with rainbow on github at http://github.com/conanite/rainbow/tree/master.

Friday, January 23, 2009

calling java from arc

Rainbow adds six builtin functions for calling java code from arc. Theoretically this is all you need to interact with any java library from arc code.

java-new (classname . args)

Create and return a new instance of classname, passing args to the constructor

java-class (classname)

Return a java-object wrapping the class identified by classname

java-invoke (obj method . args)

Invoke method on obj with args. 'defcall binds this function to java-object so that you should only rarely need to call this explicitly. Return the value of the method if there
is any, wrapping into arc types where possible.

java-static-invoke (classname method . args)

Invoke static method on classname with args.

java-static-field (classname fieldname)

Return the value of the specified static field

java-implement (interfacename strictly methods)

Returns a java-object which implements the java interface identified by interfacename, the implementation being provided by the arc code given in methods, where methods is a hash mapping method-name to corresponding implementation. The strictly parameter determines whether an error should be raised if methods does not include all of the methods defined on the interface. java-implement is necessary for implementing, for example, mouse and keyboards handlers for a desktop application, or servlets and filters for a web application.

Following in the arc tradition of using arc-code wrappers for builtin functions (eg 'w/stdout for 'call-w/stdout, 'atomic for 'atomic-invoke, 'thread for 'new-thread), rainbow provides some wrappers to make java interaction a little less ugly and possibly a little more arcish. The 'implement macro removes some boilerplate around 'java-implement, so you can create an implementation of, for example, an iterator (where xs is a list) thus:

(def to-iterator (xs)
(implement "java.util.Iterator"
hasNext (fn () xs)
next (fn () (pop xs))))

The 'bean function instantiates and configures a java object via its setter methods. Here is an example of creating a JFrame object, specifying its bounds and title:

(def frame (left top width height title)
(bean "javax.swing.JFrame"
'bounds (list left top width height)
'title title
'contentPane (box 'vertical)))

Behind the scenes, bean is going to call setBounds, setTitle, and setContentPane on a newly-instantiated JFrame object, finally returning the JFrame.

There are lots more goodies defined in rainbow's swing.arc, but that's a topic for another day.