As you can see, out of the box, you'll get a bunch of sensible files and a test that passes. Then I implemented a few functions to try out the testing suite.

https://gist.github.com/samjarman/a92ecc08e4d22161f15615d9e33d7466

Additionally, similar to Python, Elixir has doc tests which I think are super cool. You can show the doctest how to interact with it as if you were in the shell and then it asserts the output. Neato.

https://gist.github.com/samjarman/ccf4561bedc0b2d8860c3ff50c3d97f5

Experiment 3: Recursion and Private Methods

After this, it was best to get my hands wet with some good ol' fashioned functional programming. So let's create a function which sums some numbers. In imperative programming languages, you could achieve this with a total variable and a loop, but that's a big no no in functional, so we'll use recursion instead.

https://gist.github.com/samjarman/e76d1eb0bfa17834577712109dd994a3

We start off with one public function and two other, private functions (notice the defp). First we can define a function that takes a list and a total variable, where the list is pattern matched into some variables already (see the shell commands above). We'll have for use in this function a tail, a head and total.

For example, calling _sum([1,2,3], 0) would make the variables of head be 1, tail be [2, 3] and total to be 0.

We simply recurse and add it up and continue. The base case is defined above - if we're at an empty list (because the previous calls tail was an empty list) then we can stop and return the total. Sure, a bit complicated, but like all recursive programming it looks pointless on simple examples but looks amazing on harder examples, like tree traversal.

Add a few doctests, run mix test again and we see it works. Boom.

Also it seems that as a naming conventions, private functions are prefixed with _. (Update: Maybe not! Eduardo points out that a lot of Elixir is written in Elixir so that's where the conventions should come from!) Generally I find it a great idea to learn naming conventions alongside everything else with new programming languages. This'll stop the better developers cringing at your code when you inevitably ask for help :P

Experiment 4: Concurrency and Messaging

So I thought it was about time we get to the bit that makes Elixir cool, which is its concurrency model. Long story short, you can spin off threads (except don't call them that, call them Erlang or BEAM processes) trivially and send messages between them. Each process has a "mailbox" and processes communicate by sending messages to each other. Seems simple enough conceptually but obviously we're only scratching the surface of the surface :P

So let's create another process and get it to send us something.

First, we'll get a reference to the current process, by assigning it to a variable called me.