Trying coffeescript

This post was originally published on blog.omgmog.net in 2011, back when I was using Octopress. Recovered from a local backup.
blog.omgmog.net

Recently decided to give coffeescript a go, it’s nice and syntactically similar to other languages such as Python.

From the coffeescript developers:

CoffeeScript is a little language that compiles into JavaScript. Underneath all of those embarrassing braces and semicolons, JavaScript has always had a gorgeous object model at its heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way.

Here’s my first test with it, first of all a golfjs solution I wrote, using obfuscated (read: in as few bytes as possible) JavaScript:

i=input,v=i.match(/[aeiou]/gi),y=i.match(/y/gi),
output=(v?v.length:0)+(y?y.length/2:0)

And here’s the same code in coffeescript:

v = /[AEIOU]{1}/gi
y = /Y{1}/gi
a = if input.match(vow) then input.match(vow).length else 0
b = if input.match(y) then input.match(y).length*0.5 else 0
output = a+b

And the JavaScript coffeescript compiles to:

var a, b, output, v, y;
v = /[AEIOU]{1}/gi;
y = /Y{1}/gi;
a = input.match(vow) ? input.match(vow).length : 0;
b = input.match(y) ? input.match(y).length * 0.5 : 0;
output = a + b;

This post is also available in plain text

[Webmentions]

Want to reply? I've hooked up Webmentions, so give it a go!