Try CoffeeScript the Python way !

” I don’t know how people live without coffee, I really don’t !” – Martha Quinn

There has been a lot of talk about the caffeine on the web, of late ! You know, how all these days, Javascript has been boosting your webpage and stuffs like that. The whole wide wide (or the world wide web) is finally giving the behind-the-scenes warrior Javascript it’s true credits. That’s a great move.

There has also been as much talk about trans-compilers as well, of late. They claim that they make your work simpler, by giving you a more simplistic programming language/interface which finally compile to another programming language. And javascript seems to have a plethora of such transcompilers, namely DARTCoffeeScript and of late, a Microsoft’s kid too TypeScript ! Whether or not Javasript is really that tough to code in it’s bare-metal, original form is a debatable topic, I’ll not dwell into it. You can surf the web and find it out for yourself ! Personally, I tried Coffeescript a couple of days ago and all I can say was it was really worth it.

So let’s talk about Coffescript now. The official page defines Coffeescript as “CoffeeScript is a little language that compiles into JavaScript.” Well, it’s truly “little”, as anyone who has known atleast one programming language can learn it in a day’s time. And for geeks who love scripting languages like Ruby or Python, there is hardly anything in Coffeescript that they really need to learn ! This is one language the scripters will love ! As I was perusing through the numerous tutorials on the web, I was enchanted by the basic simialrity between Coffeescript and Python. So I propounded to make a small tutorial to help Pythonistas learn Coffeescript the “Python-way”, and here it is, you’ve already chosen to peruse !

Python is a simple, yet powerful programming language. So keeping this as a base, let me try to show you how easy it is to move over to Coffeescript and make your way to becoming a great web coder ! I won’t promise that you’ll become a Coffeescript ninja at the end of this, however, you’ll still be in a good position to appreciate Coffeescript and pave your way into the stardom-cafe! (that’s the name of a imaginary hub of Coffescript-ing geeks)

Let’s start with the basics…

1. Declaration of Variables -

Python is a dynamic programming language i.e. you don’t have to declare the variables upfront before using them. You can directly start using the variables in the code and the first occurance of the variable is considered it’s declaration.

Coffeescript also follows a very similar approach. You dont have to declare variables with the var keyword as you do in javascript. You can directly use it in your code. However you’ll be surprised to notice that the compiled javascript has the declaration of all your variable at the beginning of the program !

2.Declaration of Functions -

Python - The syntax to declare a function in Python is

def function_name(param1, param2,...):
//your code goes here

Coffeescript - The syntax to declare a function in Coffeescript is

function_name = (params_list) ->
//your code goes here

Guess it is not too much of a difference here from Python … Just don’t forget the -> (hifen followed by an arrow )

This eventually compiles to

display = function(params_list) {
//your code goes here
}

3. Conditional Flows - 

Now that we have declared our vars & funcs, let’s dive deeper. Let’s consider you want to check if the season is summer and then predict the weather conditions thereafter.

In Python, i’d do it this way -

if season == "summer" :
weather = "hot"

Now that is child’s play ! Let’s see what Coffeescript has got to offer here

Actually, you can do this check and assignment is 3 different ways in Coffeescript !
a. The traditional way …

if season == "summer"
weather = "hot"

b.

weather = "hot" if season is "summer"

c.

if season is "summer" then weather is "hot"

Now that is a lot like the way you speak, isn’t it !

OK, so what if the season is not “summer” ?

In Python

if season == "summer" :
weather = "hot"
else :
weather = "cold"

I pretty much think that by now, you can guess what it might be like in Coffeescript

a. Traditionally…

if season == "summer"
weather = "hot"
else weather = "cold"

b. With a tinge of Coffee, it’d be …

if season is "summer" then weather = "hot"
else weather = "cold"

Now let’s look at this from the opposite perspective ! Let’s introduce “unless”

unless season is "summer"
weather = "cold"
else weather = "hot"

4. Array

Let us play with Arrays now.

In Python, this is how you would declare and print the contents of an array

arr = [1,2,3,4,5]
for i in arr :
print i

Declaring arrays whose contents are consecutive numbers is a lot simpler in Coffeescript…

arr = [1..5] #array with inclusive range
# it compiles to arr = [1,2,3,4,5]
arr = [1...5] #exclusive range
# it compiles to arr = [1,2,3,4]

Now for printing the contents of the array

<strong>for i in arr
console.log i

Getting both the index and value of the array is also equally simpler :

arr = [1..5]
for value, ind in arr
console.log "arr[#{ind}] = #{value}"
#Output :
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5

One of the major strengths of Python lies in it’s array splicing and slicing capabilities

arr = [10,20,30,40,50]
new_arr = arr[0:3]
# new_arr = [10,20,30]

Let’s see how Coffeescript features on array splicing and slicing

arr = [10,20,30,40,50]
newArr = arr[0...3]
# from 0th element upto 3rd element (non-inclusive range)
# newArr = [10,20,30]

More examples :-

Python

new_arr = arr[ :2] #from beginning upto 2nd element
#new_arr = [10,20]

Equivalent in Coffeescript

newArr = arr[...2]

Python

new_arr = arr[3: ]
# from 3rd element till the last element
# new_arr = [40, 50]

Equivalent in Coffeescript

newArr = arr[3...]

Take a sip of Coffee and enjoy Coffeescripting !
Pythonista or not, do let me know what you think of Coffeescript.

About these ads

One thought on “Try CoffeeScript the Python way !

  1. I’m totally excited about coffeescript now! I checked out the site and messed around with python scripting in coffeescript to see how similar the two are and at first I just thought they were just similar but now I can see how coffeescript out does python!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s