Stok Footage

Continually experimenting with new ideas and techniques — Reconstructing, Developing, Modernising.

Elixir the Language

I’ve started working with the language Elixir for fun after many months of meaning to have a go at it. The experience to date has been great. I’ve long been intending to get some experience with functional programming, having used some of Ruby’s functional methods in an object oriented world. Elixir seems a good starting point for understanding the difference in approach between functional and object oriented programming.

I’ve had some fun updating someone’s mandelbrot image generating program to use more than one core on my laptop. Stealing the pmap idea from the Pragmatic ProgrammersProgramming Elixir book soon had the fans on my laptop whirring away with the result being generated between three and four times as fast. That quick win is the kind of thing which piques my interest and helps me move along.

After that taste of success I tried out a few ways of generating Fibonacci numbers to get a feel for streams and the like. At my stage in learning this seems to be a nice way to get the nth Fibonacci number without recursion or caching:

1
2
3
4
5
6
7
8
9
10
  def fib(n) when n >= 0 and is_integer(n) do
    [0, 1]
    |> Stream.iterate(fn [a, b] -> [b, a + b] end)
    |> Stream.with_index
    |> Stream.drop_while(fn {_, i} -> i < n end)
    |> Stream.take(1)
    |> Enum.map(fn {[value, _], _} -> value end)
    |> Enum.to_list
    |> List.first
  end

The most alluring introduction to Elixir I have seen on the web is José Valim’s quick intro at howistart.org. It gets into distributed code and task supervision pretty quickly, and gives a good flavour of Elixir.

Some other sources of information and inspiration I’m working my way through (albeit at my own pace) are:

Postscript

I have also had to reacquaint myself with my vim configuration as the syntax highlighting for Elixir didn’t take properly on MacVim. Solution: a ~/.vim/filetype.vim to force the issue.

I find it interesting to move back to my preferred and familiar development tools on my laptop after many months of experience with other editors and much larger screens. That’s fodder for another post…

Tags:

Leave a Reply

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