Causes Tech: Seven Programming Languages in Seven Weeks: Erlang
Posted Oct 06, 2011 by Kristján
This week our chat was on Erlang, a language developed to be supremely concurrent and reliable. As a result, in the last 24 hours no fewer than 6 subsystems here have been looked at with the idle thought that it might be time for a rewrite. Thus is the seductiveness of Erlang.
The bits we like:
- List comprehension—Particularly those with a Python background miss this in Ruby, and it’s wonderfully concise in its power
- Pattern matching—Another terse and powerful concept that comes over from Erlang’s roots in Prolog, but it’s done much better this time around.
- First-class functions—Working so long with Ruby’s blocks and JavaScript’s closures, some good way of passing code around has become nearly a necessity in my mind. The coolest part is how nicely this plays with message passing, which I’ll discuss in a moment.
- Network communication—It just worked, which might be the first time I’ve been able to say that about anything network-related.
- Hotswapping code—This is one of the main selling points and what lets Erlang systems win the uptime olympics.
The coolest thing I discovered in my one-night-stand (so far) with Erlang was a combination of those last three: first-class functions + network + hopswapping code = So Cool. I had started with a simple echo server and gotten basic hotswapping working. With the server running, one could recompile its code, then send a signal from the client and have the new code take effect with no blip in availability. Since functions can be passed around like candy, I thought “Can I make the client send new code to the server?”, and Erlang is magical enough that it answered “Yes” and continued “gimme 5 minutes.”
The full code for this is available on Github, but here’s the relevant piece:
This is part of the server’s receive loop, which also handles echo to respond and swapto pull in changes compiled on the server. Now the client can do something like:
Erlang happily sends your code to the server and the match on {netload, Fn, Sender} will start calling Fn() instead of its original run(). Madness.
We have two more language left: Clojure and Haskell. If you want to hang out and chat about them or any of the languages we’ve covered so far (Ruby, Io, Scala, Prolog), get in touch in the comments. We’ve also got some more permanent positions available.