Adventures with #12in23 - Elixir


Elixir was mentioned to me originally by a friend as Erlangs equally interesting but more approachable cousin. So when I’ve read the Exercism newsletter and saw that this February is to be functional, I didn’t have to think too much about my choice for this months flavor. 

Elixir has been very interesting, it’s also the first language where I’m going past the 5 exercises to try and implement some of the more interesting things that latter exercises have to offer. It’s really fun to write even if it doesn’t pamper me quite like Ruby did with all that sweet syntactic sugar ( I’m seeing that few languages have a glycemic index as high as Ruby does ).

Implementation wise Elixir has quite a few cool little bits that have been front loaded in the Exercism course and have made it even more fun to write in.

Function notation including the number of their arguments after a forward slash like “function/X” when talking about a function. Once seen it just kind of makes sense,  I’m sure (Or I sure hope) there is a reason this isn’t implemented more widely but I like it. 

Freelancing life :D

In the image above we can see both normal and shorthand function notation, like most other modern languages Elixir implicitly returns the last value within function so we can use

Screenshot 2023-08-05 at 13.48.54.png 6.97 KB
Where “do: something” returns something that it does.

Swapping def for defp makes a function private.

Screenshot 2023-08-05 at 13.49.04.png 9.18 KB
Anonymous functions in Elixir are also quite interesting. 

Screenshot 2023-08-05 at 13.49.13.png 31.1 KB
Above we can see the functions secret/1 within secret_multiply/1 being declared using the reserved word fn with a -> token and an end. I have to note, assigning to variable is absolutely not necessary here.

This is visible in the anonymous function/1 within secret_substract/1 being declared using the shorthand notation with no assignment to variable. & declares the function, () scopes the function and &1 denotes first passed param(to the best of my limited understanding!). 

These anonymous functions can then be invoked by using a . between the reference and the variables passed, function.(params).

Elixir also uses an interesting list notation storing all lists as linked lists, storing the head and the remaining items as tail. 

Screenshot 2023-08-05 at 13.49.27.png 15.7 KB

Borrowed from Exercism.org readme for language-list exercise.

Last but not least we have something that is very cool but was entirely new to me, the a() |> b() operator (made by combining | and >). That can be used to pass the results of the function a to the function b like so

Screenshot 2023-08-05 at 13.49.36.png 14.6 KB

Elixir has been extremely fun overall, I’ve ended up (as planned) going a fair bit deeper than the 5 exercises and will most likely engage with the language more as it gives me the same “Ruby” kind of energy that has drawn me into this world. I’ve claimed it doesn’t pamper me quite like the good ol’ ruby but there is a certain degree of comfort and warmth that is very much still there. Elixir just feels right, you know? Added benefit, I get to make close friends with recursion, what is there not to love.

Screenshot 2023-08-05 at 13.49.47.png 29.5 KB


Google sure has its own thing going when it comes to that too!


This website uses cookies to only make you click ok here once. It doesn't collect personal data (and neither do I)