mattyw

talkative, friendly, programmer

Why I Like Elixir (and Other Go Programmers Might Too)

| Comments

My journey to elixir

Erlang was the first functional language I ever used. When I first started reading Programming Erlang by Joe Armstrong I was hooked, I was fascinated in a language where “=” didn’t indicate an assignment, and the idea of pattern matching immediately struck a chord with me, I loved the one module per ‘process’ thing. Erlang was set to become my favourite language, there was only one problem.

I didn’t like the syntax

It’s a completely personal view, I know people who love the syntax, I just didn’t. To me it just didn’t have what Christopher Alexander in “The Timeless way of building” calls “quality without a name”. I suppose I just didn’t find it fun to type. So after re writing a couple of internal libraries in erlang to see what they looked like (they were much shorter and quite easy to read) I stopped using it, and started playing around with other languages like haskell and clojure.

Clojure was my first lisp dialect and I enjoy it alot, the syntax, the whole homoiconicity thing, macro’s aren’t something you should use all the time – but it’s comforting to know they’re there to fall back on if you need them.

Parallel to this functional language experimentation was another path – languages I used at work, which has seen me recently move from writing mostly in Python to Go. Go’s channels and go routines are great, but when I started writing go code seriously (about 8 months ago) there was something that immediately stuck me.

1
2
3
4
thing, err := someFunction()
if err != nil {
    //handle the error
}

Now, I’m not going to start complaining about go’s error handling, because I like it. But sometimes I would like to be able to make use of pattern matching and do something like this:

1
{thing, nil} = someFunction()

And have my goroutine just dies if it fails. I know that’s not go’s philosophy with errors, and that’s fine. I’m just saying.

Work and Fun Collide

I’ve spent the past 6 months saying to people that I would like a lisp dialect to be built onto go. What I really meant was I wanted a language that would let me do go style concurrency and play around with a repl, macros and all that functional programming fun stuff.

It looks to me that elixir provides exactly that.

Just look at these two programs. Both call a 10 second sleeping function on a seperate ‘thread’ and wait for the response. The first in go, the second in elixir.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package main

import (
  "fmt"
  "time"
)

func longRun(response chan string) {
  time.Sleep(10e9)
  response <- "done"
}

func main() {
  response := make(chan string)
  go longRun(response)
  value := <-response
  fmt.Println(value)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
defmodule Main do

  def longRun(pid) do
    :timer.sleep(10000)
    pid <- {:result, :done}
  end

  def main() do
    spawn(__MODULE__, :longRun, [self])
    receive do
      {:result, value} ->
        IO.puts value
    end
  end

end

Main.main()

They’re not too different, elixir has spawn where go has ‘go’ and elxir has receive where go has select. There’s no denying that they’re completely different languages, in some rather crude tests I’ve done go does seem to be largely faster, but elixir let’s me do meta-programming whilst forgetting about types.

I expect to be blogging alot more about elixir in the future

Comments