Mind blowing Elixir

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.

6 Likes