Posted by Jonas Elfström Tue, 19 Apr 2011 13:46:00 GMT
CoffeeScript is inspired by Ruby and Python but what's most peculiar with it is that it compiles to JavaScript. The generated JavaScript isn't all that bad and it even passes JavaScript lint without warnings.
"Underneath all of those embarrassing braces and semicolons, JavaScript has always had a gorgeous object model at its heart." - Jeremy Ashkenas
About a week ago I stumbled on the very clever Sudoku solver by Peter Norvig. I have nothing (or at least not much) against Python but I pretty soon checked out the Ruby translations of the solver. I then refactored one of the solutions to get a chance to get to know the algorithm better.
Now that I finally installed CoffeeScript the Sudoku solver came to mind. I dived in head first and got in trouble pretty soon. It turns out that Array Comprehensions in CoffeeScript differs some from the List Comprehensions in Python.
1 2 3 |
def cross(A, B): "Cross product of elements in A and elements in B." return [a+b for a in A for b in B] |
returns an one-dimensional array if you call it with two arrays (or strings).
But in CoffeeScript
cross = (A, B) -> (a+b for a in A for b in B) |
returns a two-dimensional array.
The jury is still out on if this is intended or not but either way the array comprehensions in CoffeeScript are still very useful.
EDIT 2011-06-02
It's been decided that this is by design. The issue has been closed.
For the cross-function I ended up with
1 2 3 4 5 6 |
cross = (A, B) -> results = [] for a in A for b in B results.push a + b results |
EDIT 2011-06-02
Using `map` I could have got much closer to the Ruby version.
1 2 |
cross = (cols, rows) -> [].concat (cols.map (x) -> rows.map (y) -> y+x)... |
I'm still more of a map/reduce guy than a list comprehension ninja.
I don't know how I missed it but as Trevor pointed out below all you have to do is
coffee sudoku.coffee
1 2 3 4 5 6 7 8 9 |------+-----+-----| A | 4 8 3|9 2 1|6 5 7| B | 9 6 7|3 4 5|8 2 1| C | 2 5 1|8 7 6|4 9 3| |------+-----+-----| D | 5 4 8|1 3 2|9 7 6| E | 7 2 9|5 6 4|1 3 8| F | 1 3 6|7 9 8|2 4 5| |------+-----+-----| G | 3 7 2|6 8 9|5 1 4| H | 8 1 4|2 5 3|7 6 9| I | 6 9 5|4 1 7|3 8 2| |------+-----+-----|
Here's the generated sudoku.js.
This is the only CoffeeScript I've ever written but I already like it (more than JavaScript). Please correct me if I strayed from the CoffeeScript way.