2009년 10월 12일 월요일

Scala for Python Programmers

You might be a Python Ninja, but mastering Scala would be hard if you'r scared of real-OO languages. As "Cal Henderson" mentioned last year at djangocon: "Python is for Hippies who scared of real-OO languages". To some extent its true because Python gives you an illusion that you are doing everything in object but most of the time you'r just iterating in script. However, if you knows what exactly are classes and objects then you can write better code.

I truely hates Java and JVM like languages why? because it slow down my laptop else? because they got bloody eclipse which is not much different than Visual Studio .NET 200x and lastly? I am not wearing suits. I hate JVM languages as much as I hate Ruby. But sometime you come across a turning point and ask yourself, what you do? hmm.. I writes program in Python .. so does everyone else on planet earth and then you think probabaly I should learn another language .. then you prepare a list C++, Java, C#, smaltalk, Ruby, Groovy, clojure, Lisp, Erlang and Scala.  And eventually scala seduce you with JVM, CLR, Scalability, industrial need and Ha! no one actually knows Scala. But wait its JVM language? hell yes but I have mastered art of programming and I can learn anything decent( I got this illusion by writing cutting edge programs in Python in last few years).

All you need is Google "Scala tutorial", you install Scala compiler and write your HelloWorld.scala - so cool :)

object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}

But its not really cool as you digg in you see competely different world and you ask yourself, is it worth learning scala? I can do this in Python in 2 lines. In short all you need to learn scala is Patience if you'r a Python or Ruby or Smalltalk programmer.

Damn! why the hell Scala guys made it too hard, they couldn't do this in Python like syntaxs? Please do not ask me this question ask EPFL guys. Please do not lost yout Patience while reading and learning scala programs.

Python, Ruby, Groovy, Smalltalk and Javascript is Dynamically Typed languages that makes them more productive than Scala which is a Staically Typed language. Difference is in Python you do something like this

>>> name = "Jon"
>>> print name
Jon
>>> name = 1
>>> print name
1

A value is bound to the Variable and type is bound to the value. In static typing type is bound to the Variable.

scala> var name = "Jon"
name: java.lang.String = Jon

scala> name = 1
<console>:5: error: type mismatch;
 found   : Int(1)
 required: java.lang.String
       name =1

So Remember, Scala = Statically Type language just like C++ which you might have learnt in school. I know it sucks but Scala guys should have some reason for it. Remember be Patient and try some meditation ;)

If you are a Python programmer then you might be assuming that you knows FP (Functional Programming) but I would reject your claim unless you know Lisp or Erlang or Scala's Actor. FP is just not about operation on list. It's more about
communicate by passing messages between concurrent, autonomous processes both Lisp and Erlang does that. To ease this thing FP often go with immutable state, i.e you cannot modify value of a variable. The coolest thing about Scala is that it support fully FP using Actors library and it also allows mutable and immutable type of variables. So, if you think Erlang is pretty cool think again in scala it is much more easier to write inter-process programs. BTW Python is lame enough to be not compared in this section.

//Import Pretty similar to Python where _ resemblance * e.g from scala.actors.Actor import *
scala> import scala.actors.Actor._
import scala.actors.Actor._

scala> import scala.actors.Actor
import scala.actors.Actor


// Define a class Acting, here Unit resemble Void or None in Python
scala> class Acting extends Actor {
     | def act() {
     | loop {
     | receive {
     | case 1 => println("yes")
     | case 0 => println("no")
     | }
     | }
     | }
     | }
defined class Acting

//create acting object
scala> var ac = new Acting()
ac: Acting = Acting@c42b5 //
Acting@node_name

//start acting
scala> ac.start
res4: scala.actors.Actor = Acting@c42b5

//send value to acting object
scala> ac ! 1
yes


scala> ac ! 0
no



It's Actor library allows you to write concurrent FP.

If you loves python mixin composition in classes then You have Scala Traits. It is much similar to Python modules.

In python you write anon function or so called lambda functions like

>>> g = lambda x: x * x
>>> g(3)
9


But in Scala you do

scala> var g = (x: Int) => x * x
g: (Int) => Int = <function>

scala> g(3)
res46: Int = 9


"=>" is oftenly used for anon functions in scala. It can be re-written as:

>>> def g(x):
>>>    return x*x


and in scala

scala> def g(x: Int): Int = x * x

Lets write another Python and Scala Program that would conver list of strings into Upper case: Python

>>> a = ['Hello', 'World', 'Python', 'Scala']
>>> up = [item.upper() for item in a]
>>> print up
['HELLO', 'WORLD', 'PYTHON', 'SCALA']


In Scala:

scala> var a = List("Hello", "World", "Python", "Scala")
a: List[java.lang.String] = List(Hello, World, Python, Scala)

scala> var upp = for(x <- a) yield x.toUpperCase
upp: List[java.lang.String] = List(HELLO, WORLD, PYTHON, SCALA)


See how Scala force you to use generators :)

Let me summarize:

1. Be Patient if you'r a Python, Ruby or smalltalk developer. Scala would require lots of Patience.
2. Scala is a Statically typed language. Watch your types.
3. Scala Actor library allows you to write server-client, concurrent Programs just like twisted and stackless do.
4. Scala is fully Functional Programming language like Lisp and Erlang.
5. => is used for anon functions (lambda in python terminology) and = is used in writing proper functions.






댓글 없음:

댓글 쓰기