Mind blowing Elixir



How has Elixir blown (or expanded!) your mind? Please tell us here!

Can be something that’s unique to Elixir, or just something that it introduced to you. Can be something you discovered a while ago… or something you just come across - whatever it is, please share :023:


Well it was mind blowing enough when I first came across recursive functions, but I’m just reading Learn Functional Programming with Elixir (Pragprog) and I’ve just met a recursive function …inside a recursive function :icon_eek: :003: :lol:


This might be a little bit tangential. Finding this in the OTP source really blew my mind. Make me realise even the most impressive software is made by normal people


Completely changed the way I think about code…which is kind of a pain when I have to use other languages…


Things that I had heard of but I was scared to try. Recursive functions. Enum.reduce and Enum.map functions. I have seen those in javascript but I was avoiding them over for loops.


This talk by Saša Juric is one of my go-to’s when trying to get people interested in Elixir. Being able to attach a REPL to a running production system and having all of these introspection capabilities at your disposal is something I don’t think any other runtime offers (or if they do it likely isn’t as deeply integrated with the runtime as you get with elixir/erlang).


This is a simple one but my mind was blown when I first saw this line of code:

“def calculate(arr) when length(arr) <= 1 do”

I’ve never seen conditional declaration of functions before. Still haven’t gotten over how cool it is

def calculate(arr) when length(arr) <= 1, do: # ...

could be expressed as

def calculate([]), do: # ...
def calculate([elem]), do: # ...

which is a bit easier to understand (at least for me, since I can’t think of a function which would handle both an empty list and a list with a single element in it the same way), and probably slightly faster.


Not specifically Elixir but BEAM’s preemptive scheduler especially after working a lot with Node is truly mind blowing.


I am constantly blown away by how so much is achieved by so little code. Thank you, thank you, thank you!


Haha new to the forum so I’m not used to where everything is and I definitely thought your username was the opening to your comment for a second or two :joy:


Not just Elixir, but because of it I would say the runtime and otp, to think that a bunch of tech were already there and nobody saw it is at least intriguing and now I barely need a bunch of infrastructure libraries, proccess is there, cache is there, kv storage is there even a database, and nodes without all of the complexity.


otp(supervisor trees/ genservers) is what blew my mind about the language. Completely changed how I wrote my code. Much stuff I was using other services for like Redis or sqs I do all in code now and its great!


Pattern matching in Elixir is amazing! I’ve never before seen such powerful stuff in other languages.


I have to say pattern matching and guards changed the way I write code and I miss them in every other language that I use (mainly JS)


Definitely pattern matching. It truly changed the way I think about code and it’s the kne thing I wish I had in every other programming language going forward.

Second to that railway oriented programming with the with expression


It’s interesting that pattern matching in Elixir is such a primitive notion (just like in erlang, of course). Actually, Elixir could have implemented pattern matching with macros. I have implemented a special form of “pattern matching” myself in my megadef library. It shows how easy it would have been to implement other forms of pattern matching inside elixir

EDIT: There are very good reasons not to do what I’ve done in my package by default, of course. It’s good for some situatuons but a disaster in others.


A picture paints a thousand words - here’s the code, reproduced with the kind permission of @ulissesalmeida :003:

def ascending([]), do: [] 
def ascending([a]), do: [a] 
def ascending(list) do
  half_size = div(Enum.count(list), 2)
  {list_a, list_b} = Enum.split(list, half_size) 
defp merge([], list_b), do: list_b
defp merge(list_a, []), do: list_a
defp merge([head_a | tail_a], list_b = [head_b | _]) when head_a <= head_b do
  [head_a | merge(tail_a, list_b)]
defp merge(list_a = [head_a | _], [head_b | tail_b]) when head_a > head_b do
  [head_b | merge(list_a, tail_b)]

So it was actually a recursive function inside a recursive function while also calling the outer recursive function :101: ascending -> merge -> ascending :star_struck:


In terms of syntax, nothing in Elixir really blows my mind, no… seriously, everything is expected. Just functions and more functions and pipe of functions and more functions, some with syntax, some comprehensions, yeah nothing really fancy. No pointers, no instance variable (that can be changed on other methods/functions). Just modules of pure functions. Protocol and behaviour are a bit harder to think about though.

Phoenix is a bit more surprising, because it has some itty bitty magic on the MVC thing. Other than that, nothing really surprises me.

It is pretty different when I code in Javascript, like everyday, something surprises me out of nowhere.
“How does it work like that?” :confused:

In terms of performance and especially concurrency, Elixir really blows my mind.


My favorite: iolists have some very interesting performance characteristics: https://www.bignerdranch.com/blog/elixir-and-io-lists-part-2-io-lists-in-phoenix/