Coffeescript for Python programmers

By : Shabda Raaj

I just learnt Coffeescript, and as a Python programmer loved being able to write Javascript in a Python like language. Coffeescript is inspired by Python/Ruby and is very close to these languages. Writing Coffeescript and reading the compiled Javascript also improved my understanding of Javascript. Without much ado here is translation of some Python code to Coffeescript to get you started.

Defining a variable

Python
a = 10

Coffeescript
a = 10

Setting scope is done via whitespaces.

Python

if i == 10:
    foo()

Coffeescript

if i == 10
    foo()

No semicolons

List comprehensions

Python

languages = ["Python", "Coffeescript", "Java", "Ruby", "Haskell"]
languages = [lan.lower() for lan in languages]

Coffeescript

languages = ["Python", "Coffeescript", "Java", "Ruby", "Haskell"]
languages = (lan.lowerCase for lan in languages)

Get a range

Python
tillTen = range(1, 10)

CoffeScript

tillTen = [1...10]

String Interpolation

Python

language = "Python"
greet = "I love %s" % language

Coffeescript

language = "Coffescript"
greet = "I love #{language}"

Multi Line String

Python

"""
The Quick Brown
Fox, Jumps over
The lazy Dog
"""

Coffeescript

"""
The Quick Brown
Fox, Jumps over
The lazy Dog
"""

Defining a function

Python

def double(x):
    return 2 * x

Coffeescript

double = (x) -> 
    2 * x

Here is a larger python script converted to Coffeescript.

You will see that they are very similar and if you know Python learning (and loving) Coffeescript won't take you a long time. Excited? Get started here.


Resources


Can you believe that it has been only an year since the 1.0 release!


Related Posts


Can we help you build amazing apps? Contact us today.

Topics : coffeescript

Comments

joaquin 24th Dec., 2011

Is there a typo in the example of string interpolation ? language <-> languages

commmenttor
Shabda Raaj 24th Dec., 2011

@joaquin: Thanks, fixed.

commmenttor
Senthil Kumar B

Nice to know something during the End of the Year ...

Knowing things around the technology you work definitely improves a lot .

Thanks for bring awareness about CoffeeScript .

commmenttor
arbie

TIL you can make cofeescript use backbone.js... now that's some clean mvc ;-)

commmenttor
Shabda Raaj 24th Dec., 2011

@Arbie: You can make Coffescript use backbone, spine, jQuery, underscore.js, anything.

The golden rule of CoffeeScript is: "It's just JavaScript".

commmenttor
Steve Howell 29th Dec., 2011

One little gotcha for Python programmers: list comprehensions work a bit differently in CoffeeScript than Python.

First, if you have conditionals in your list comprehensions, you want to use the "when" keyword, not "if."

Second, if you have nested comprehensions (i.e. multiple "for" loops), then CoffeeScript produces nested arrays, whereas Python essentially flattens the iteration. I much prefer Python's behavior in this regard. Fortunately, it's mostly a non-issue for me, as I tend to use comprehensions only for simple iterations and then go back to explicit loops and pushes for more complicated mappings:

See more here: https://github.com/jashkenas/coffee-script/issues/1191

Despite this gotcha, CoffeeScript will feel right at home to many Python programmers. It helps to have a little bit of love for Ruby, too, but most of the Ruby-isms are optional.

commmenttor
pyro 17th Feb., 2012

The CoffeeScript example for list comprehensions is incorrect.

The correct method for converting a string to lower-case in JavaScript is String.prototype.toLowerCase and it must be called within the comprehension with ().

[current]
languages = ["Python", "Coffeescript", "Java", "Ruby", "Haskell"]
languages = (lan.lowerCase for lan in languages)

[corrected]
languages = ["Python", "Coffeescript", "Java", "Ruby", "Haskell"]
languages = (lan.toLowerCase() for lan in languages)

commmenttor
Cathal

The ranges are slightly incorrect: Python counts from x but stops before y, whereas Coffeescript counts from x to y inclusive.

So in Python:
[x for x in range(1,10)] => [1,2,3,4,5,6,7,8,9]

but in Coffeescript:
(x for x in [1..10]) => [1,2,3,4,5,6,7,8,9,10]

commmenttor
Post a comment Name :

Email :

Your site url:

Comment :

© Agiliq, 2009-2012