Thursday, March 11, 2010

Using Jarc with App Engine

I've written a sample Jarc web app for Google App Engine.

Overview of the code


  id Long
  description String
  rating String
  created Date
This uses SML format to define the JDO object to persist in App Engine. The script src/mkjdo.arc converts this into a Java class.


(use 'jtml)

(def /home (req res)
      (title "WineRecord - What wines did I like?")
      (link rel "stylesheet" src "winerecord.css")
      (script src "")
      (script "$(document).ready(function() { $(\"input:text:first\").focus(); });"))
        (with-open db (getPersistenceManager pmf*)
          (form action "/add" method "POST"
              (let wines (execute (newQuery db "select from wine.Wine order by created desc range 0,500"))
                (tr (th "Buy Again?") (th "Wine") (th "Date Added") (th))
                (each wine wines
                  (tr (td wine!description)
                      (td wine!rating)
                      (td wine!created)
                      (td (a href (string "/edit?id=" wine!id) "Edit")))))
              (tr (td (text name "description" size 56))
                  (td (select name "rating"
                              (option "yes")
                              (option "no")
                              (option "maybe")))
                  (td (submit value "Add Wine"))
        (a href "/export" "Export Wines") " "
        (a href "/import" "Import Wines")))))

The above is just an excerpt. You can also view the complete winerecord.arc

No, it doesn't use the standard arc html package. I think it would be difficult to make that work since it would require serializing closures to make them available in multiple web servers.

The jtml package uses SML format which has a direct mapping to XHTML. So the tag names should be familiar to anyone familiar with HTML. The only additions to HTML in jtml are abbreviations for input tags based on the type, for instance you can do:

(text name "name" value "foo")
instead of requiring
(input type "text" name "name" value "foo")
Although the latter works also.

Given my rant about the advantages of saving one token you might wonder why I didn't abbreviate the html tags to within and inch of their lives. Well, in this case, I think leveraging compatibility with HTML is more valuable. And you can create your own macros to abbreviate it however you like. It doesn't have to be part of the Jarc language, unlike the Java access syntax which does have to be part of the language.


Jarc has it's own HttpRequest class which supports lookup using apply, so the code can do:

   ... req!rating ...
Instead of
   ... (getParameterValue req "rating") ...


The JDO class created from src/wine.sml is in package "wine". See build.xml where mkjdo is called (around like 32). The second argument to mkjdo is the Java package name to use. I couldn't get App Engine to work if the JDO object was in the default package.

Features of the JDO Wine class

  • Supports lookup using apply - wine!created
  • Supports sref - (= wine!created ...)
  • Has a constructor that takes a map to init the object - (new wine.Wine (getParameterMap req))
  • Has a putAll method to update the object - (putAll wine (getParameterMap req))
So if the HTML form has the same names as the JDO object it is easy to create or update the JDO object from a form.

Source code and Running App

You can download the entire source from and you can play with the running app at

Wednesday, March 10, 2010

Java access syntax from Jarc - less dots, more filling

Have you ever wondered if the ease of calling C libraries could be responsible for a lot of Python's popularity? C function calls look just like native Python calls.

import george;

You can't tell from the Python code whether the module, george, is written in C or Python. It doesn't matter to the calling program. That's a simple foreign function interface.

Jarc brings this same simple foreign function interface to Arc. Unlike Clojure and JScheme, the syntax for calling a Java method is the same as for calling any Lisp function.

Jarc> (getTime (new java.util.Date))
Jarc> (getTime "foo")
Error: Symbol 'getTime' has no value

Even though there is no function getTime defined, that function can still be called on a Date instance.

Jarc uses dispatch on first arg to figure out how to evaluate the method call. This was suggested by Paul Graham in Arc at 3 Weeks. Although Arc doesn't currently have dispatch on first arg it is ideal for Jarc to access Java methods.

If you've defined classes in Python (or Perl), this may seem intuitive.

class HelloClass:
    def f(self):
        return 'hello world'
That self there is the first argument to the function. Even though you call the function as x.f() what's happening under the covers is that x is passed as the first argument. This same thing is happening under the covers in Perl and C++.


1. You can treat Arc calls and Java calls exactly the same

Polymorphism, anyone? Here's the Jarc macro with-open, which is just like let except that it also calls close on variable. It is slightly more complicated then that because is always calls close even if there is an error. And it ignores any errors that might happen when calling close.

(mac with-open (var init . exprs)
   `(let ,var ,init
      (protect (fn () ,@exprs)
        (fn () (errsafe (close ,var))))))
This is quite handy and ensures that your "stream" gets closed, both when it is an arc type:
(with-open f (outfile "what.ever")
Where Jarc calls the Arc function close, and when the "stream" is a Java object:
(with-open db (java.sql.DriverManager.getConnection ...)
Where Jarc calls the close method on the java.sql.Connection instance.

2. Java objects work with map

No helper function (like memfn in Clojure) is needed to use Java instance methods with map.

(map 'getTime '(list (new java.util.Date)))
Both map and apply accept a symbol (in addition to a function, of course) and interpret that as a Java method call.

3. One less character

And of course, since succinctness is power, saving one whole character is an advantage as well. Clojure requires you to type an additional period.

(.getTime (new java.util.Date))
Astute readers will know that succinctness is defined by the number of nodes in the parse tree. And the Clojure example above still has the same number of nodes as the Jarc version. But the number of nodes is also a proxy for how hard it is to read the code. Our brains have to process the code too. And I think parsing .getTime requires parsing the dot separately. And it's not useful information. Just like in Python, I don't want to be distracted with extra syntax to indicate that this is a Java method call. It's just a function call and should be just as simple.