ANN: scriptjure, a library for generating javascript

6 views
Skip to first unread message

Allen Rohner

unread,
Oct 7, 2009, 10:01:55 PM10/7/09
to Clojure
Hello, I'd like to announce the availability of a new library, called
Scriptjure. It's a macro that generates javascript strings from
Clojure s-exprs. My initial use for it is in glue code for Clojure
webapps. For example:

(js (fn foo [x]
(var y 3)
(return (+ x y))))
=> "function foo (x) {
var y = 3;
return (x + y);
}"

You can get the code and read the documentation at http://github.com/arohner/scriptjure

Allen

Jarkko Oranen

unread,
Oct 8, 2009, 1:50:21 AM10/8/09
to Clojure
I took a quick look, and that looks really neat. One thing though:
Instead of the "clj" form to escape back to clojure, why not just use
"unquote" (and possibly unquote-splicing)? that would allow you to use
the ~ reader macro as a shortcut, just like in syntax-quote.

--
Jarkko

Meikel Brandmeyer

unread,
Oct 8, 2009, 2:18:27 AM10/8/09
to Clojure
Hi,

On Oct 8, 7:50 am, Jarkko Oranen <chous...@gmail.com> wrote:

> I took a quick look, and that looks really neat. One thing though:
> Instead of the "clj" form to escape back to clojure, why not just use
> "unquote" (and possibly unquote-splicing)? that would allow you to use
> the ~ reader macro as a shortcut, just like in syntax-quote.

+1

Sincerely
Meikel

Emeka

unread,
Oct 8, 2009, 5:38:32 AM10/8/09
to clo...@googlegroups.com
Allen,

Great job!

Did you try 
(defmulti emit class) ?

Regards,
Emeka

Allen Rohner

unread,
Oct 8, 2009, 11:37:22 PM10/8/09
to Clojure
It was a minor thing actually, I didn't like having to quote "user"
code, and AFAIK, it's not possible to add a quasiquote as part of a
macro. i.e. in my code, there is a function (_js) that does the heavy
lifting, and the macro (js). I couldn't write:

(defmacro js [forms]
(quasiquote (_js forms))

That and IMO, (clj foo) is slightly more readable than ~foo.

Allen

Allen Rohner

unread,
Oct 8, 2009, 11:38:04 PM10/8/09
to Clojure
> Did you try
> (defmulti emit class) ?

That is a good point. I'm actually not sure why I used type there.
It's probably a bug. Thanks for pointing it out.

Allen

On Oct 8, 4:38 am, Emeka <emekami...@gmail.com> wrote:
> Allen,
> Great job!

Meikel Brandmeyer

unread,
Oct 9, 2009, 3:10:28 AM10/9/09
to Clojure
Hi,

On Oct 9, 5:38 am, Allen Rohner <aroh...@gmail.com> wrote:

> That is a good point. I'm actually not sure why I used type there.
> It's probably a bug. Thanks for pointing it out.

I would leave type here. Then the user can override the emit-function
if he has self-defined types which need special handling for whatever
reason.

Sincerely
Meikel

Meikel Brandmeyer

unread,
Oct 9, 2009, 3:22:46 AM10/9/09
to Clojure
Hi,

On Oct 9, 5:37 am, Allen Rohner <aroh...@gmail.com> wrote:

> It was a minor thing actually, I didn't like having to quote "user"
> code, and AFAIK, it's not possible to add a quasiquote as part of a
> macro. i.e. in my code, there is a function (_js) that does the heavy
> lifting, and the macro (js). I couldn't write:
>
> (defmacro js [forms]
>    (quasiquote (_js forms))
>
> That and IMO, (clj foo) is slightly more readable than ~foo.

I'm not sure what you mean with the first paragraph. We do in
clojureql exactly the same (with a slightly different implementation,
though). That's fine.

As for the clj form: I would prefer ~, because it allow to be
consistent across different DSLs: syntax-quote uses it, eg. clojureql
uses it, scriptjure could use it also (Not very representative, but I
hope get the meaning). For the user the advantage is, that he doesn't
have to learn something new. It always the same principle, it always
used the same way. You now chose clj. The next implementer chooses
dequote. Another one inject. Or whatever. So for each thing you have
to remember how to use it.

I think one strength of Clojure is its immense consistency:

(alter foo + 2)
(swap! foo + 2)
(send foo + 2)
(alter-meta! foo update-in [:x] + 2)

This is easy to remember because it's always the same principle. Now
suppose it would look like this:

(alter foo #(+ 2 %)) ; takes no additional args, just a function
(swap! + foo 2) ; function goes first
(send + 2 foo) ; agent goes last

It would be really great to get such consistency also across
libraries, eg. by using ~ for the case at hand.

Sincerely
Meikel

PS: Actually I think quasiquote would be a nice addition to quote and
syntax-quote in core.
Reply all
Reply to author
Forward
0 new messages