Thoughts

Those Who Compile Into JavaScript

I wondered what it would be like to have my front-end workflow in Ruby, or Common Lisp. What about all the libraries that could have been easily bowered and required in JavaScript? Could existing libraries somehow fit into the picture, or should there be counterparts taking their places? What about CoffeeScript, how does it work?

After some research, the list of answers I got:

 

Q: Starting from an example of JavaScript compiler–how could CoffeeScript be written in CoffeeScript?

The original version of the CoffeeScript compiler was written in Ruby, but it was transitioned to CoffeeScript for v0.5.0, on Feb 21, 2010 [1].

 

Q: Why do it?

Several reasons [2]:

  • demonstrate the usefulness of said language
  • take advantage of the features of said language
  • having a bootstrapped (recursive) compiler makes it easier to upgrade the language

 

Q: The idea is interesting. So how does CoffeeScript work with libraries like jQuery and RequireJS?

In CoffeeScript we use:

jQuery ->
  ### your code here ###

Which in JavaScript compiles to [3]:

jQuery(function(){
  /* your code here */
});

Likewise,

require ["jquery"], ($) ->
 console.log "it works!"

compiles to:

require(["jquery"], function($) {
  return console.log("it works!");
});

From how it looks, CoffeeScript is merely JavaScript. One simply needs to write in the correct CoffeeScript Syntax.

 

Q: Could this rule of CoffeeScript be extended to any other languages that could compile into JavaScript? In my particular case, Ruby and Common Lisp?

The question it seems, is whether there are reliable compilers for certain languages. In ruby’s case, Opal seems to be a popular choice (unfortunately libraries don’t seem well supported, there is even a opal-jquery for Opal). Common Lisp, well, even less standardised (CL is huge for one thing), but Parenscript is one, and Lispysript seems nice too.

 

Q: Why do you even want to do it, considering, quote, as a rule of thumb, each layer of interpretation costs a factor of 10 in speed [4]?

Because “Inefficient software isn’t gross. What’s gross is a language that makes programmers do needless work. Wasting programmer time is the true inefficiency, not wasting machine time. This will become ever more clear as computers get faster.” Because I’m curious. I wish for a better future, in which we could all freely express what we want to achieve. As for now, I could peep into that future by playing around with CoffeeScript a bit.

 

Fun read: List of Languages that Compiles into JavaScript

Standard