Language War – Scala versus Python

No – I am not going to discuss the theoretical pros and cons of Scala and Python. Instead, I am going to talk about the practical issues startup founders like me face in establishing our main language.

The context: The core of ZLemma.com is a mathematical framework involving highly structured data representations and numerical algorithms. The product involves a large relational database and is delivered through a web application. We wanted to move fast with our development, but we also wanted to build for scale and robustness. A compiled language like C++ was not in consideration because dev progress would be too sluggish. On the other hand, a web programming language like PHP was also not in consideration because it simply wouldn’t work for mathematical modeling.

Initial Thoughts: My first thought was Haskell. You say, “Haskell? Are you crazy?” Crazy – You got that right :) It’s a very beautiful language and once you start coding in Haskell, you tend to get religious about functional programming. Lately, Haskell has become a fairly pragmatic language and is gaining popularity because of the emergence of functional programming paradigms in mainstream languages. However, people talked me out of it – although I am still considering Haskell as a choice for the future :)

The next thought was Scala. Scala is inspired by Haskell and is a more pragmatic functional programming language because of its interop with Java. I decided that Scala will be our core language and that we will program in Scala in the “Haskell-style”.

When Zubin (our Python Guru) joined the team, he began selling me on Python. I remember my impatient persona telling him, “That’s great, but you are going to do Scala here”. Zubin is not religious about Python and appreciates functional programming, so he started learning Scala. After a couple of days, I realized I am being bull-headed and that I should give Python serious consideration. After all, many startups have built sophisticated functionality with Python. And who the hell does Scala? (Actually, Twitter does!)

After some analysis, I realized that Python and Scala are more similar than I had assumed (functions are first-class objects in Python, after all). However, the differences are significant. Here are the five factors I took into account before deciding on our main language.

Developer comfort: We’ve been fortunate to have a very strong dev team. Each of us can pick up new languages and paradigms quickly. Ajit is a very careful engineer – he prefers object-orientation and static typing. Gaurav comes from a mathematical computing background like me and so he is more comfortable with functional programming and static typing. But generally, both Ajit and Gaurav are fairly neutral in their attitude to languages. As should be obvious by now, I am pretty obsessive about functional programming and static typing.

Zubin is the classic hacker in the team, so it was important that we give Zubin a language that he is comfortable with. Zubin enjoys Python’s duck typing and swears by “Code like a Pythonista”. I paid close attention to this fact :) He also enjoys the elegance of list-comprehensions in Python (that made me happy!). Finally, I had to think about future developers we’ll hire. Hmmm … suddenly, it looked like Scala is losing this battle and it did. 1-0 to Python.

Fine – it’s just a battle. I was hoping Scala will win the war.

Typing: My first thought here was : “If you are doing any serious algorithms or anything mathematical, dynamic typing is just not on. When we define functions in Math, we first talk about the domains. The domain specification effort is always well-worth it – you need to know the space you are operating in, otherwise the Math goes out the window”. I heard myself and thought: “That’s the academic in me talking”. More practically, it’s a question of safety. This amounts to playing defense. Good defense is very important to me, even though playing defense slows down the pace of dev progress. Dynamic typing is like speeding on the highway on a motorbike – exciting and dangerous. Static typing is like riding in a military tank – safe and slow. Neither is ideal. The solution is somewhere in-between.

Either inferred static typing (Haskell, Scala) or libraries for automatic type assertions in a dynamic language (Typecheck in Python). My view has been that the former works without slowing you down much. Typecheck in Python sort of defeats a key philosophy of Python: duck-typing. Besides, dynamic typing scares the hell out of me. I’d rather be a bit slow and safe than fast and dead (a.k.a. chicken!). Scala wins this battle. 1-1.

Time Out. The remainder of the story is here.

Ashwin Rao
Co-founder, ZLemma.com

ps: Discuss on Hacker News (please be civil!)

14 comments

  1. Steve says:

    Since you mention that you like Haskell, I have to ask you if you are confortable with the following. Try to guess the output before you see it. I personally was shocked by this, which I only happened across a couple of days ago when doing some mathematical work (scipy is tempting – I’m moving on to Julia though).

    >>> monomials = [lambda x: x**i for i in [1,2,3]]

    OK, what will these functions evaluate to at the point 2.0?

    >>> monomials[0](2),monomials[1](2),monomials[2](2)
    (8, 8, 8)

    Apparently Python captures the single mutable cell (memory location) for “i” in the closures, which they all share. Unlike if the lambda had been an actual function composition. Python fans say this is in the spec (I’m sure it is), but I think it’s brain damaged.

    Here’s a workaround by the way (hope you don’t ever forget to apply it):
    >>> monomials2 = [lambda x,i=i: x**i for i in [1,2,3]]
    Now everything’s fine:
    >>> monomials2[0](2),monomials2[1](2),monomials2[2](2)
    (2, 4, 8)

  2. Nick G says:

    Have you considered Golang (AKA Go)?

    It feels a lot like Python to program in but has inferred static typing and is compiled to native binaries.

    • Angel M says:

      Go is more a c ish language than a functional one, yes it has lambda but it miss the generics which are important in functional languages and avoid the existence for high order functions.. For a functional programmer go feels so much empirical repetitive and not so powerful.. It is not a bad language but programmers coming from c or java tends feel it better than programmers coming from functional Languages, the go type system is a bit weak…..

  3. Matt Campbell says:

    I think there’s one other factor you’re overlooking, and that’s concurrency. Python has the global interpreter lock, which means in effect that only one CPU at a time can be active for a given Python process, no matter how many threads that process has. You could get around this with a process per CPU core, but that involves some amount of memory overhead, and more processes to restart when you deploy new code. You may find the JVM more efficient and easier to work with in this regard.

    • Sandeep says:

      You could use Java, although it’s not as paulpor. If you’re making a game from scratch (no engine), the most paulpor is C++ by far. However C++ is a hugely complicated language (to the point where Google’s style guide for C++ has don’t use it for several features) and it’s really easy to mess up and leak memory (something you don’t need to worry about in Java). So many game engines prefer that you use a scripting language. Valve’s Source Engine does require you to code in C++ (and I believe the id Tech Egnine, which Source is derived from, does as well), but most of the others don’t. Unity3D, a newer graphics engine designed for cross-platform use including the web and smartphones, allows you to use a custom Javascript-based language, C# (Microsoft’s answer to Java), or Boo (a scripting language inspired by Python). The Unreal Engine, which is probably the most paulpor engine used in major games such as Bioshock and Gears of War, has its own custom UnrealScript that you use for most coding.

  4. Javier says:

    Aside from typecheck, you might want to have a look at pycontracts as well:
    https://pypi.python.org/pypi/PyContracts/

  5. […] about the practical issues startup founders like me face in establishing our main language… >>Language War – Scala versus Python | ZLemma's Blog […]

  6. […] is a sequel to my earlier post – Language War – Scala v/s Python where I shared some of the practical and emotional/instinctive factors that come into play when […]

    • Anisha says:

      the more tools the merrier!php is wiledy used in all webapps today. Also there’re some project from facebook team to optimize and compile in native c++ language any php script (HipHop for PHP) .I think python is beatiful for it’s semplicity, and you should use whatewer you feel comfortable with, but it’s better to have more ace up your sleeve! Was this answer helpful?

  7. “Dynamic typing is like speeding on the highway on a motorbike – exciting and dangerous. Static typing is like riding in a military tank – safe and slow. Neither is ideal. The solution is somewhere in-between.”

    So language choice depends on the problem to solve. Use Scala to fight the russians and Python for a sunday afternoon ride.

    I personally find myself coding less on the server and more in the browser and there is no alternative do dynamic typing anyway.

  8. Angel M says:

    I’m not sure about python as a functional language, yes it has lambda but the one line limitation is a bit annoying, the python code looks more like a terse c++ than a functional, except for the list comprehension python code is so empirical, the normal and classic python code has a lot of variable declarations and personally I feel than ruby is much more functional..

    Now, I can’t understand why you said that dynamic languages are not good for complex algorithm, the most important scientific languages are dynamical, since lisp until python, passing for R or matlab t

Leave a Reply

Your email address will not be published. Required fields are marked *