##Intro## Elixir has been on my radar for about a year now, however despite my best effort I haven’t had a chance to take a thorough look yet. I decided I’ve been waiting long enough, I just need to sit down and try to hack out some code. If you haven’t looked into the language yet it appears very cool on paper:
Elixir is a functional, concurrent, general-purpose programming language built atop the Erlang Virtual Machine (BEAM). Elixir builds on top of Erlang to provide distributed, fault-tolerant, soft real-time, non-stop applications but also extends it to support metaprogramming with macros and polymorphism via protocols.
I have a little bit of familiarity with Erlang, but besides that this is the first time I’ve actually looked into the Elixir syntax. So the rest of this post will be entirely, unfiltered stream of consciousness.
##Trying Out Some Code## Lets see if we can use Elixir to solve the first problem on the Project Euler site.
Multiples of 3 and 5: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.
First lets try to fire up the REPL, I think I remember front page mentioning it was called iex.
Great, so lets try to start working on the problem. Seems like we can naively solve this by traversing a list, lets see if we can figure out the syntax for that. Maybe it’s like Haskell? [1..10.]
Hrmm… that didn’t work. Maybe lets try dropping the brackets? I think i vaguely remember Erlang only using two dots in the notation?
That looks like it could be a representation of a list! I wonder how we could take the head it?
Lets try just a function?
Maybe it’s prolog/erlang style, using the symbolic syntax?
Looks like that’s not right… something seems off I thought that would work… It seems like the language is probably erlang with some syntax. I vaguely remember the head function in erlang being hd/1, let’s try that on our list.
Hrm… it seems like the 1..1000 syntax doesn’t create a list, maybe it’s a symbol? Time to go read the docs …
So after reading the docs for lists it seems like, the bracket syntax is correct. hd/1 is head as we thought and tl/1b is tail. However they don’t seem to cover the range syntax on that page. Need to dig in to the docs a little bit more I guess, maybe there is no range syntax?
Ahha! It looks like on this page that we were actually kind of correct. 1..1000 seems to represent a lazy range, not a list. So we need to use functions to operate on top of the lazy range.
Looks like there is a Stream package for lazy sequences, which has map/filter:
Need to figure out how to write a lambda, lets try to filter out odd numbers.. one of the map examples in the docs page uses this syntax:
Seems to make sense, lets try it out.
I would have thought that would have worked… wait the parenthesis don’t look right. I forgot to properly close the rem(..)!
I think I saw a Enum.sum function as well, putting all the pieces of the question together we get a nice one liner.
That wasn’t so bad, I like the pipelining syntax. It seems similar to the syntax used in ML based language, like F#/OCaml.
If you are on board with Elixir and want to continue on your own Elixir journey there are a couple of really great resources to continue exploring the language.
- How I Start: Elixir
- Elixir-Lang Getting Started Tutorial
- The 30 Days of Elixir Repository
- Learning X in Y Minutes: Where X = Elixir
- The Elixir Cheat Sheet